Generating random numbers in computers

Khan MNM Sadh
Sep 4, 2018 · 3 min read

Generating random numbers is an interesting field in computer science and in computer engineering.

When I first needed and used a random number generator, I was faced with astonishment when the random numbers it generated wasn’t random at all. I vaguely remember the numbers it generated were something like 42, 37, 4, 31, …, and was always so. I was using C++ rand() function for it, and this random number generator always takes the same seed. So, I cooked up a way to generate truly random-ish seed and posted about it later here: Randomizing the C++ Random function. This method generates a predictable random number at a given time and given that it was still using a computational method to generate, what are essentially pseudo random numbers.

Pseudo Random Number Generators (PRNG) have been used since the early days of computing. But, those don’t cut the mustard well enough. You see, any computational method will generate predictable sequence of numbers. Some can argue, that, following is a perfectly fine non-uniform PRNG:

XKCD:221

If you want to know how PRNGs work, you can study some of the well known algorithms from this list of random number generators. One of the earliest known such algorithms is the middle-square method, suggested by John von Neumann in 1946. Here follows an overview of the algorithm from Wikipedia:

1. take any number,

2. square it

3. remove the middle digits of the resulting number as the “random number”

4. then use that number as the seed for the next iteration.

For example, squaring the number “1111” yields “1234321”, which can be written as “01234321”, an 8-digit number being the square of a 4-digit number. This gives “2343” as the “random” number. Repeating this procedure gives “4896” as the next result, and so on.

Von Neumann used 10 digit numbers, but the process was the same.

Since PRNGs won’t actually generate truly random numbers, engineers are using various physical sources to generate random numbers. For example, various crypto key generators make users randomly move mouse pointers to generate random numbers. These work somewhat like this: A Mouse Pointer Random Number Generator.

Random.org provides API to get true random numbers that are generated from atmospheric noise.

Recently there have been technological advancements in commercially producing Hardware random number generators (also known as True Random Number Generators or TRNGs). Intel introduced an Assembly command RDRAND with IvyBridge processors in 2013 and AMD added support for the same command in 2015. This would use processor’s built in TRNG hardware to yield random numbers.

Such TRNGs use microscopic physical phenomena that are stochastic and theoretically unpredictable, such as thermal noise, photoelectric effect etc.


See also


Originally published at www.quora.com.

inside the insight

inside the insight - posts by nafSadh

Khan MNM Sadh

Written by

http://nafSadh.com

inside the insight

inside the insight - posts by nafSadh

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade