The SwiftRNG API Kit ships with device cluster scalability and fail-over capabilities. With this, it is possible to use multiple SwiftRNG devices to additively increase the random number generation speed. By doing so, the reliability of the cluster is increased. This API feature seamlessly integrates multiple devices and uses them concurrently as a single stream of entropy. The API will monitor the health of the cluster and will resize the cluster on-the-fly, allowing device swapping in real-time. This makes it possible to remove and add SwiftRNG devices in the middle of random number generation. For best results, all devices in a cluster should have same generation speeds and the system should have available a CPU/MCU core (or a hyper thread) per device. The following C sample code will create a connection to a cluster of multiple SwiftRNG devices for retrieving random numbers.
A sample source code in C for retrieving bytes and integers from a cluster of two SwiftRNG devices
/*
* sample-cl.c
* Ver. 2.6
*
* This is a sample C program that demonstrates how to retrieve random bytes
* from a cluster of two SwiftRNG devices using 'swrng-cl-api' API for C language.
*
*/
#include <swrng-cl-api.h>
#define BYTE_BUFF_SIZE (10)
#define DEC_BUFF_SIZE (10)
/* Allocate memory for random bytes */
unsigned char random_byte[BYTE_BUFF_SIZE];
/* Allocate memory for random integers */
unsigned int random_int[DEC_BUFF_SIZE];
/****************
* Main entry
****************/
int main() {
int i;
double d;
unsigned int ui;
SwrngCLContext ctxt;
printf("-------------------------------------------------------------------------------------------\n");
printf("--- Sample C program for retrieving random bytes from a cluster of two SwiftRNG device ----\n");
printf("-------------------------------------------------------------------------------------------\n");
/* Initialize the context */
if (swrngInitializeCLContext(&ctxt) != SWRNG_SUCCESS) {
printf("Could not initialize context\n");
return(1);
}
/* Open the cluster if any SwiftRNG device if available */
if (swrngCLOpen(&ctxt, 2) != SWRNG_SUCCESS) {
printf("%s\n", swrngGetCLLastErrorMessage(&ctxt));
return(1);
}
printf("\nSwiftRNG cluster of %d devices open successfully\n\n", swrngGetCLSize(&ctxt));
/* Retrieve random bytes from cluster */
if (swrngGetCLEntropy(&ctxt, random_byte, BYTE_BUFF_SIZE) != SWRNG_SUCCESS) {
printf("%s\n", swrngGetCLLastErrorMessage(&ctxt));
swrngCLClose(&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)random_byte[i]);
}
/* Retrieve random integers from the cluster */
if (swrngGetCLEntropy(&ctxt, (unsigned char*)random_int, DEC_BUFF_SIZE * sizeof(unsigned int)) != SWRNG_SUCCESS) {
printf("%s\n", swrngGetCLLastErrorMessage(&ctxt));
swrngCLClose(&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 = random_int[i] % 99999;
d = (double)ui / 100000.0;
printf("random number -> %lf\n", d);
}
printf("\n");
swrngCLClose(&ctxt);
return (0);
}
The C code sample presented above, is included as part of the SwiftRNG Software API Kit which can be downloaded at this address.
To build and test the sample code with provided Makefile using Linux command prompt, locate ...linux-and-macOS/swrng
directory and enter the following commands (you may need to run ./sample-cl with sudo
permissions):
make sample-cl
./sample-cl
The output from running the code above may look like this:
-------------------------------------------------------------------------------------------
--- Sample C program for retrieving random bytes from a cluster of two SwiftRNG device ----
-------------------------------------------------------------------------------------------
SwiftRNG cluster of 2 devices open successfully
*** Generating 10 random bytes ***
random byte 0 -> 123
random byte 1 -> 190
random byte 2 -> 204
random byte 3 -> 147
random byte 4 -> 163
random byte 5 -> 179
random byte 6 -> 97
random byte 7 -> 167
random byte 8 -> 68
random byte 9 -> 13
*** Generating 10 random numbers between 0 and 1 with 5 decimals ***
random number -> 0.242170
random number -> 0.802870
random number -> 0.250980
random number -> 0.903650
random number -> 0.670670
random number -> 0.291010
random number -> 0.733100
random number -> 0.532130
random number -> 0.728430
random number -> 0.977520