The following demonstrates how to use C language for downloading random data from SwiftRNG device when used with SwiftRNG Software API.

A sample source code in C for retrieving bytes and integers from SwiftRNG device

#include "stdafx.h"

#include "swrngapi.h"

#define BYTE_BUFF_SIZE (10)
#define DEC_BUFF_SIZE (10)

unsigned char randombyte[BYTE_BUFF_SIZE]; // Allocate memory for random bytes
unsigned int randomint[DEC_BUFF_SIZE]; // Allocate memory for random integers

//
// Main entry
//
int main() {
        int i;
        double d;
        unsigned int ui;
        SwrngContext ctxt;
        printf("--------------------------------------------------------------------------\n");
        printf("--- Sample C program for downloading random bytes from SwiftRNG device ---\n");
        printf("--------------------------------------------------------------------------\n");

        // Initialize the context
        if (swrngInitializeContext(&ctxt) != SWRNG_SUCCESS) {
                printf("Could not initialize context\n");
                return(1);
        }

        // Open the first (device number 0 is the first device) SwiftRNG device if available
        if (swrngOpen(&ctxt, 0) != SWRNG_SUCCESS) {
                printf("%s\n", swrngGetLastErrorMessage(&ctxt));
                return(1);
        }
        printf("\nSwiftRNG device open successfully\n\n");

        // Download random bytes from device
        if (swrngGetEntropy(&ctxt, randombyte, BYTE_BUFF_SIZE) != SWRNG_SUCCESS) {
                printf("%s\n", swrngGetLastErrorMessage(&ctxt));
                swrngClose(&ctxt);
                return (1);

        }

        printf("*** Generating %d random bytes ***\n", BYTE_BUFF_SIZE);
        // Print random bytes
        for (i = 0; i < BYTE_BUFF_SIZE; i++) {
                printf("random byte %d -> %d\n", i, (int)randombyte[i]);
        }

        // Download random integers from device
        if (swrngGetEntropy(&ctxt, (unsigned char*)randomint, DEC_BUFF_SIZE * sizeof(unsigned int)) != SWRNG_SUCCESS) {
                printf("%s\n", swrngGetLastErrorMessage(&ctxt));
                swrngClose(&ctxt);
                return (1);
        }

        printf("\n*** Generating %d random numbers between 0 and 1 with 5 decimals  ***\n", DEC_BUFF_SIZE);
        // Print random bytes
        for (i = 0; i < DEC_BUFF_SIZE; i++) {
                ui = randomint[i] % 99999;
                d = (double)ui / 100000.0;
                printf("random number -> %lf\n", d);
        }

        printf("\n");
        swrngClose(&ctxt);
        return (0);

}

The output from running the code above may look like this:

--------------------------------------------------------------------------
--- Sample C program for downloading random bytes from SwiftRNG device ---
--------------------------------------------------------------------------

SwiftRNG device open successfully

*** Generating 10 random bytes ***
random byte 0 -> 36
random byte 1 -> 95
random byte 2 -> 63
random byte 3 -> 216
random byte 4 -> 162
random byte 5 -> 242
random byte 6 -> 243
random byte 7 -> 35
random byte 8 -> 35
random byte 9 -> 232

*** Generating 10 random numbers between 0 and 1 with 5 decimals  ***
random number -> 0.007890
random number -> 0.778770
random number -> 0.225020
random number -> 0.647540
random number -> 0.138170
random number -> 0.655630
random number -> 0.993020
random number -> 0.176640
random number -> 0.676990
random number -> 0.301260