The following demonstrates how to use C++ language for downloading random data from AlphaRNG device when used with AlphaRNG Software API.
A sample source code in C++ for retrieving bytes and integers from AlphaRNG device
#include <AlphaRngApi.h>
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
const int count = 10;
unsigned char rnd_byte_buffer[count];
AlphaRngApi rng; // Using default constructor to achieve maximum security data connectivity.
cout << "------------------------------------------------------------------------------" << endl;
cout << "--- Sample C++ program for retrieving random bytes from an AlphaRNG device ---" << endl;
cout << "------------------------------------------------------------------------------" << endl;
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** Generating " << count << " random bytes ***" << endl;
// Retrieve random bytes from device
if (!rng.get_entropy(rnd_byte_buffer, sizeof(rnd_byte_buffer))) {
cerr << rng.get_last_error() << endl;
return -1;
}
for (int i = 0; i < count; i++) {
cout << "random byte " << i << "-> " << (int)rnd_byte_buffer[i] << endl;
}
return 0;
}
The output from running the code above may look like this:
------------------------------------------------------------------------------
--- Sample C++ program for retrieving random bytes from an AlphaRNG device ---
------------------------------------------------------------------------------
AlphaRNG device open successfully
*** Generating 10 random bytes ***
random byte 0-> 66
random byte 1-> 143
random byte 2-> 145
random byte 3-> 3
random byte 4-> 120
random byte 5-> 42
random byte 6-> 239
random byte 7-> 15
random byte 8-> 64
random byte 9-> 147