Generating true random numbers on a PC

Thread Starter

dcbingaman

Joined Jun 30, 2021
855
I have read about true random number generators that plug into a USB port on a PC. I have an application that requires the generation of random numbers (1000 or more per second). Looking for either a 32bit integer or even better a 64bit integer. Does anyone have any experience with these devices or other methods to generate random numbers? I am familiar with the standard C libraries that generate pseudo-random numbers using rand() and srand() but these are of limited value at creating true random numbers. Thanks in advance for any help and or experience in this matter.
 
Last edited:

Papabravo

Joined Feb 24, 2006
19,852
I have read about true random number generators that plug into a USB port on a PC. I have an application that requires the generation of random numbers (1000 or more per second). Does anyone have any experience with these devices or other methods to generate random numbers? I am familiar with the standard C libraries that generate pseudo-random numbers using rand() and srand() but these are of limited value at creating true random numbers. Thanks in advance for any help and or experience in this matter.
Donald E. Knuth devoted a chapter in his series on the Art of Computer Programming, Vol 2., Chapter 3 to this question. The upshot was that natural processes are inherently random, but numerical algorithms on a computer cannot be truly random. Pseudo-random is as good as it gets.
 
Last edited:

Thread Starter

dcbingaman

Joined Jun 30, 2021
855

nsaspook

Joined Aug 27, 2009
10,914
I have read about true random number generators that plug into a USB port on a PC. I have an application that requires the generation of random numbers (1000 or more per second). Looking for either a 32bit integer or even better a 64bit integer. Does anyone have any experience with these devices or other methods to generate random numbers? I am familiar with the standard C libraries that generate pseudo-random numbers using rand() and srand() but these are of limited value at creating true random numbers. Thanks in advance for any help and or experience in this matter.
What type of random do you need? For statistical randomness most pseudo-random numbers generators are fine when seeded properly. For security uses a proper https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator is needed.
 

Thread Starter

dcbingaman

Joined Jun 30, 2021
855
What type of random do you need? For statistical randomness most pseudo-random numbers generators are fine when seeded properly. For security uses a proper https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator is needed.
It is for a high speed simulation of a 'species' (Biology) where random mutations occur to 'genes' within the population. The probability of a mutation for each gene being very low (1 in 100,000 or less). Being the population is large and for the simulation to run accurately, a lot of these low probability numbers need to be generated each second of time. (well above 1000 or more per second). I do not require one for security reasons. I think one of the problems I was running into with using pseudo random numbers was using an appropriate 'seed'. Normally I would use the computer time in milliseconds for the 'seed' and this works fine for applications that do not require random numbers at high speeds, but maybe there is a better method for generating truly random, seeds?
 

nsaspook

Joined Aug 27, 2009
10,914
It is for a high speed simulation of a 'species' (Biology) where random mutations occur to 'genes' within the population. The probability of a mutation for each gene being very low (1 in 100,000 or less). Being the population is large and for the simulation to run accurately, a lot of these low probability numbers need to be generated each second of time. (well above 1000 or more per second). I do not require one for security reasons.
Then 'repeatable' random sequences might be a requirement. You could use the USB device as a random seed generator (reusable keys) for a good pseudo-random generator that uses something like this.
https://en.wikipedia.org/wiki/Mersenne_Twister
 

BobTPH

Joined Jun 5, 2013
6,302
Normally I would use the computer time in milliseconds for the 'seed' and this works fine for applications that do not require random numbers at high speeds, but maybe there is a better method for generating truly random, seeds?
This makes no sense. The seed is used once. Why would the rate of getting random numbers affect how you get the seed?
 

ZCochran98

Joined Jul 24, 2018
225
IIRC Beta decay is one of the processes that exhibits true randomness.
Brownian motion and thermal noise are, too. You can even take advantage of the inherent thermal noise and recombination noise in semiconductors to create a random number generator (or, rather, random signal generator which could then be fed to an ADC)
 

Thread Starter

dcbingaman

Joined Jun 30, 2021
855
This makes no sense. The seed is used once. Why would the rate of getting random numbers affect how you get the seed?
I agree. Correction to the sentence should read 'truly random number' not seed. Actually for game applications, I have used nothing more than the time in milliseconds for when the user 'moved the mouse or pressed a button' and to the modulus of that for what I was interested in. This created a truly random number. Example rolling a dice I would get the time in milliseconds and mod that with 6 for a number between 0 and 5 for example. Of course that method does not work for generating a lot of random numbers per second.
 

WBahn

Joined Mar 31, 2012
28,182
It is for a high speed simulation of a 'species' (Biology) where random mutations occur to 'genes' within the population. The probability of a mutation for each gene being very low (1 in 100,000 or less). Being the population is large and for the simulation to run accurately, a lot of these low probability numbers need to be generated each second of time. (well above 1000 or more per second). I do not require one for security reasons. I think one of the problems I was running into with using pseudo random numbers was using an appropriate 'seed'. Normally I would use the computer time in milliseconds for the 'seed' and this works fine for applications that do not require random numbers at high speeds, but maybe there is a better method for generating truly random, seeds?
Why does the rate at which you need pseudo-random numbers matter in terms of using a see. Please don't say that you are reseeding the random number generator every time you need another number?
 

Thread Starter

dcbingaman

Joined Jun 30, 2021
855
Why does the rate at which you need pseudo-random numbers matter in terms of using a see. Please don't say that you are reseeding the random number generator every time you need another number?
It does not matter, that is my mistake. It may very well be a simple use of rand() and srand() functions is sufficient except that rand() returns a number for 0 to 32767 which if you want some probability that is less than 1/32767 it will need some modifications.

My system returns 32767 for the following:

printf("Max random on my system is: %d\n",RAND_MAX);
 
Last edited:

MrSalts

Joined Apr 2, 2020
2,767
It does not matter, that is my mistake. It may very well be a simple use of rand() and srand() functions is sufficient except that rand() returns a number for 0 to 32767 which if you want some probability that is less than 1/32767 it will need some modifications.

My system returns 32767 for the following:

printf("Max random on my system is: %d\n",RAND_MAX);
You can use the least significant decimal places ( 8- or 10- or 12 -digits) of your system time stamp if your code is not running in synch with a 1-second interval or other repeatable time. You can do some random e sampling and plot them to look for patterns vs an equal distribution of values from 0-1.
 

WBahn

Joined Mar 31, 2012
28,182
It does not matter, that is my mistake. It may very well be a simple use of rand() and srand() functions is sufficient except that rand() returns a number for 0 to 32767 which if you want some probability that is less than 1/32767 it will need some modifications.

My system returns 32767 for the following:

printf("Max random on my system is: %d\n",RAND_MAX);
IF the underlying statistical properties of the built in PRNG are good enough for your application (and they may or may not be), then you can get pseudo-random numbers of any size you want just by concatenating repeated calls.

my15bitRand = rand();
my30bitRand = (rand()<<15) + rand();
my45bitRand = ( ((rand()<<15) + rand())<<15 ) + rand();
my60bitRand = ( ( ( ((rand()<<15) + rand())<<15 ) + rand())<<15 ) + rand();

Because of the sea of parens, it's probably better to just do:

my15bitRand = rand();
my30bitRand = (rand()<<15) + rand();
my45bitRand = (rand()<<30) + (rand()<<15) + rand();
my60bitRand = (rand()<<45) + (rand()<<30) + (rand()<<15) + rand();

EDIT: Updated to reflect that, in C, the << operator has higher precedence than +.
 
Last edited:
Top