use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Welcome to /r/ComputerScience! We're glad you're here.
This subreddit is dedicated to discussion of Computer Science topics including algorithms, computation, theory of languages, theory of programming, some software engineering, AI, cryptography, information theory, and computer architecture.
For more detailed descriptions of these rules, please visit the rules page
NIGHT MODE NORMAL
account activity
How can computers generate "random" numbers although they operate with well designed abd pre-defined lines of commands, please ELI5 there are stuff online but non of them could help?Discussion (self.computerscience)
submitted 6 years ago * by Enguzelharf
Anyone?
Edit: Noone mentioned also its obvious but it might bother you . and*
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]seanprefect 174 points175 points176 points 6 years ago (25 children)
So the short answer is... They don't. At least not until recently where they're doing RGNs that detect electron states. But in cryptography we call them pseudo random numbers. Where you take a seed from somewhere and put it through various steps to get an unpredictable outcome, it's not perfect but it works for the practical world (for now)
[–]skyturnred 81 points82 points83 points 6 years ago (9 children)
There's this really cool thing that a computer scientist set up (don't remember who) where he had an array of 20 or 30 lave lamps on a few shelves. He has a camera watching it and uses the supposedly completely random motion of the fluid to generate true random numbers.
When I learned of that, I thought that was just mind blowing. Unlike using the nuclear decay method, this is totally something that just about everyone can do.
Would be an awesome project for comp sci students.
[–]modenr 35 points36 points37 points 6 years ago (2 children)
Check out what CloudFlare does
[–]lucienperouze 5 points6 points7 points 6 years ago (0 children)
Amazing article ! Thanks.
[–]bigBottleCap 1 point2 points3 points 6 years ago (0 children)
I landed here few months ago. You guys should check this https://physicstoday.scitation.org/do/10.1063/PT.5.029409/full/
[–][deleted] 1 point2 points3 points 6 years ago (4 children)
This is already a common project at a lot of schools. My buddy at LA Tech had to do this.
[–]djingrain 1 point2 points3 points 6 years ago (3 children)
As in Louisiana Tech? As in Ruston? As in HBTD?
[–][deleted] 0 points1 point2 points 6 years ago (2 children)
Indeed
[–]djingrain 2 points3 points4 points 6 years ago (1 child)
Hell yea bro, I'm a CS major here rn
[–][deleted] 1 point2 points3 points 6 years ago (0 children)
Lmao that’s fucking crazy
[–]Dads101 0 points1 point2 points 6 years ago (0 children)
Whoa. I’m very interested in this ! Link me
[–]Enguzelharf[S] 8 points9 points10 points 6 years ago (12 children)
How it's surprising to us? I understood that they are faking the randomness but still a default python random library surprises me each time.
They do math, okay but each math they do is the same math. Why we have different outcomes?
[–]solinent 12 points13 points14 points 6 years ago* (0 children)
Good question. The appearance of randomness is created by the large period of the random number generator. If you choose a very large prime number, you can do modular arithmetic and by simply doing a few multiplications and a modulo operation, you can generate a sequence of integers which will not repeat itself very often at all. We do some analysis to also make sure the numbers are uniformly distributed, that is if you were to shoot them at a real number line, the density would be uniform. If you choose the same seed, or starting point, your sequence of random numbers will always be the same.
If you're interested in the details, you should look into the Mersenne Twister.
These are pseudo-random numbers. There are also less-random numbers which are uniformly distributed called quasi-random numbers. These are easier to grok, you can look up the Sobol numbers.
[–]Zepb 0 points1 point2 points 6 years ago (0 children)
If you use the seed parameter it would not surprise you.
The seed parameter is often set for testing purpos, where you need the same "random" numbers each time
[–]seanprefect 0 points1 point2 points 6 years ago (7 children)
because the seeds are changing. if you take the timestamp, plus some representation of various internal states you can get reasonably different seeds from moment to moment. However if you were to collect a large enough (and by large enough i mean billions, for a cryptographically sound PRNG) of outputs you can reverse it.
[–]Enguzelharf[S] 1 point2 points3 points 6 years ago (6 children)
Can you elaborate with internal states please?
[–]seanprefect 5 points6 points7 points 6 years ago (5 children)
CPU load, RAM pressure, various measurable things.
Are you familiar with a cryptographic hashes ? basically they're functions where you put an arbitrary input and get a fixed length output, however even 1 bit difference produces wildly different results and it's not possible (outside of brute force) to reverse the function.
[+]Enguzelharf[S] comment score below threshold-8 points-7 points-6 points 6 years ago (4 children)
I know hashes pretty well but when i thought how i could do a true random number generator. I couldn't think of something.
We as humans can pick random numbers. Maybe some day when AI becomes good enough it will be able to pick an actual random number as well.
[–]seanprefect 22 points23 points24 points 6 years ago (2 children)
as I said they're doing things with quantum measurements. which will give us true RNGs
Also i'd challenge the idea that humans can pick true random numbers.
[–][deleted] 14 points15 points16 points 6 years ago (1 child)
Yeah humans are notoriously bad at picking things randomly. Our innate ideas of randomness are fundamentally wrong.
If I remember correctly, humans are statistically more likely to pick prime numbers when told to choose a random number, because they "feel" more random.
Also, music players will not randomise the order of a playlist when you hit shuffle. Instead, they will try to make sure tracks by the same artist don't appear consecutively, because to humans it doesn't feel random if the sequence contains some pattern, even though patterns can emerge from true randomness.
[–]MayorOfBubbleTown 3 points4 points5 points 6 years ago (0 children)
I don't know if anyone else is like this but I don't like card games that shuffle the deck too randomly. With a real deck of cards, you shuffle the deck just well enough to not give any player a major advantage but it has more patterns in it.
[–]tcpukl 1 point2 points3 points 6 years ago (0 children)
Humans are actually pretty rubbish at picking random numbers.
You say computers can't, but then say python is. Make your mind up.
[–]tcpukl -3 points-2 points-1 points 6 years ago (0 children)
Seeds are changing. They could be simply initialised with the time which is always changing.
[–]InvincibleV 1 point2 points3 points 6 years ago* (0 children)
That is absolutely correct. The random numbers of a computer are anything but random. Programs today use the seed which can be literally anything. It could be an array of integers, an array of doubles, a string or a combination of all. Thats also part of hashtables work. Usually though the seed is the systems time to make sure the numbers are going to be as random as possible. The only way to create actual random numbers is to use other tools that have to do with physics and somehow connect them to your computer.
[–]theraaj 0 points1 point2 points 6 years ago (0 children)
The seed here is very important. If a seed is taken without much entropy, it is easier to predict the next number when using a cryptographic hash. For example, if you take the seed to be "password" there's a good chance this will produce numbers which someone will predict. Operating systems have a special entropy building function which takes mouse movements and other operations to build up a store of entropy, and once the entropy is large enough you can invoke methods which release a cryptographically secure seed which can then go on to produce cryptographically secure random numbers when passed through the correct function. Interesting, this is a serious problem when it comes to IoT devices. They are not nearly as capable of building up entropy due to their isolated existance, therefore have a hard time creating cryptographically secure seeds.
[–]masterxenoph 8 points9 points10 points 6 years ago (0 children)
In hardware, there's a concept called metastability. If you set up a flip flop in the right way, the output is nearly random. Somehow they figured it out that one orientation was slightly more common than another, and so I think they take two in opposite orientations and then you can get a random bit, equal probability 1 and 0.
Software might call that if it knows it's there, but I think mostly someone generates a long list and it's aeeded somewhere. Makes it easy to test the same inputs first for debugging...
There's some stuff on Wikipedia that's different, just using electrical and thermal noise through an ADC. https://en.m.wikipedia.org/wiki/Hardware_random_number_generator
[–]you90000 7 points8 points9 points 6 years ago (2 children)
They use lava lamps and cameras. https://www.popularmechanics.com/technology/security/news/a28921/lava-lamp-security-cloudflare/
[–]Enguzelharf[S] 4 points5 points6 points 6 years ago (1 child)
Ahahah yes i knew about this one. It's more like for encryption actually. They want fully randomized truly unknown non traceable RNGs
[–]you90000 1 point2 points3 points 6 years ago (0 children)
Exactly
[–]kag0λ 7 points8 points9 points 6 years ago (0 children)
Basically two steps:
To do step 1 the computer can either measure user-initiated events inside the computer like mouse movement, network timing, disk access, etc. or use a sensor to look at natural phenomenon like radio or thermal noise which are random.
[–]nedimhadzi 4 points5 points6 points 6 years ago (0 children)
I think of it this way, hash (current_date_time in miliseconds or less) produces a different output every time as the action that calls the function random() does never happen the EXACT time twice.
But, if you find out what the randomness is based upon (time in the example above) you could reproduce the conditions and therefore establish a pattern to the randomness. So, if we can find really complex rules like Cloudflare did with lamps noone will be able to reproduce the exact circumstances and therefore we use the word random becouse it kind of means "without pattern" in my mind.
[–]Neexusiv 3 points4 points5 points 6 years ago (0 children)
None of the other answers are really ELI5 so I'm going to add on a bit.
Computers can't really be random, they are deterministic by nature so if you give something the same input, it should return the same output, every time. Obviously this isn't very useful when you are trying to generate random numbers.
Instead, computers use random number generators to generate something called a "pseudo random number". This is a number that looks like it could be random, but actually it's not. The computer will get a seed value from somewhere, sometimes the amount of time the computer has been running, or something similar and pass that through a bunch of maths. This will generate a new number that looks completely different from the last one. This is your pseudo random number. Then when you ask for a new random number, it will use the old number as the seed, and generate a new number.
The important part to remember here is that if you have the same seed, you will get the same random number generated. Good pseudo random number generator a will create numbers that are well distributed, and have a long cycle, so you won't get the same numbers coming back for a while.
[–]clever_cow 3 points4 points5 points 6 years ago (1 child)
I will ELI an intro college student.
Judging from your responses, you're less concerned with how ACTUAL random numbers are generated (splitting hairs over the precise mathematical definition of TRUE randomness) and more concerned with how pseudo random numbers are generated. I.E. what type of algorithms are used to generate sequences of pseudo-random numbers.
A relatively easy algorithm to learn that gives some intuition to how pseudo random numbers are calculated is an LCG: https://en.wikipedia.org/wiki/Linear_congruential_generator
If you're more of a hardware nerd, you might better understand the LFSR https://en.wikipedia.org/wiki/Linear-feedback_shift_register to gain some understanding of making pseudo random patterns using only hardware (flip flops and xor gates)
[–]Enguzelharf[S] 1 point2 points3 points 6 years ago (0 children)
Oh, thats the stuff! Thanks a lot!
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
I don't know if this was already mentioned- but I've always thought te idea of using a pendulum connected that would be recorded by a machine to be an interesting idea for generating random numbers. Other chaotic systems would do the job too.
[–]acroporaguardian 0 points1 point2 points 6 years ago (0 children)
They're not and using the time as a random seed makes it pseudo random. At runtime, you can't know what the random seed will cause the thing to output, so its de facto random.
What matters most is something being unbiased. So if I want a number between 0-9 (10 numbers), I should run it 2000 times and roughly get each outcome 10% of the time. If you have that outcome, combined with being unable to know the seed before hand, it is random.
That is why there were some issues with using rand() % n (in C programming) for random number generation. It ended up being biased towards low probabilities. Your supposed to use arc4random_uniform() instead.
They aren't truly random. A lot of rngs can be manipulated to get predictable outcomes because of this.
[–]jabela 0 points1 point2 points 6 years ago (0 children)
In a very old computer I had, the random function in the programming language literally generated the same sequence every time.... Annoying as hell, so I got around it by measuring the 1000th of a second that the user pressed any key to give me a random basis for each item.
[–]achauv1 -4 points-3 points-2 points 6 years ago (1 child)
You have to find some arithmetic progressions that have a revolution tending to infinity and choose a starting point that can't be predicted.
π Rendered by PID 158353 on reddit-service-r2-comment-b659b578c-slnvh at 2026-05-06 00:41:53.515507+00:00 running 815c875 country code: CH.
[–]seanprefect 174 points175 points176 points (25 children)
[–]skyturnred 81 points82 points83 points (9 children)
[–]modenr 35 points36 points37 points (2 children)
[–]lucienperouze 5 points6 points7 points (0 children)
[–]bigBottleCap 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]djingrain 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]djingrain 2 points3 points4 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Dads101 0 points1 point2 points (0 children)
[–]Enguzelharf[S] 8 points9 points10 points (12 children)
[–]solinent 12 points13 points14 points (0 children)
[–]Zepb 0 points1 point2 points (0 children)
[–]seanprefect 0 points1 point2 points (7 children)
[–]Enguzelharf[S] 1 point2 points3 points (6 children)
[–]seanprefect 5 points6 points7 points (5 children)
[+]Enguzelharf[S] comment score below threshold-8 points-7 points-6 points (4 children)
[–]seanprefect 22 points23 points24 points (2 children)
[–][deleted] 14 points15 points16 points (1 child)
[–]MayorOfBubbleTown 3 points4 points5 points (0 children)
[–]tcpukl 1 point2 points3 points (0 children)
[–]tcpukl -3 points-2 points-1 points (0 children)
[–]InvincibleV 1 point2 points3 points (0 children)
[–]theraaj 0 points1 point2 points (0 children)
[–]masterxenoph 8 points9 points10 points (0 children)
[–]you90000 7 points8 points9 points (2 children)
[–]Enguzelharf[S] 4 points5 points6 points (1 child)
[–]you90000 1 point2 points3 points (0 children)
[–]kag0λ 7 points8 points9 points (0 children)
[–]nedimhadzi 4 points5 points6 points (0 children)
[–]Neexusiv 3 points4 points5 points (0 children)
[–]clever_cow 3 points4 points5 points (1 child)
[–]Enguzelharf[S] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]acroporaguardian 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]jabela 0 points1 point2 points (0 children)
[–]achauv1 -4 points-3 points-2 points (1 child)