HOW DO RANDOM NUMBER ALGORITHMS WORK??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

HOW DO RANDOM NUMBER ALGORITHMS WORK???

Since computer stuff are always accurate and always deliver a fixed result, its kind of perplexing how it could be random😕

16th Oct 2017, 7:46 PM
Poet🎭
Poet🎭 - avatar
2 Answers
+ 5
You are right! Computer are accurate and deliver a fixed result. But a computer can make random results if you give it a ever changing variable. The current time is a very good and ever-changing variable to use as a seed for your randomness needs. One example is like in C++ there is a rand function that generate a random output. Though the rand function will have the same output every time you run the program, if you have a static seed for the function. But if you set the current time as the seed, the output will be different every time you run the program. So, basicly to have total randomness in computers you need to have values that are changing. Hope this helps you!
16th Oct 2017, 8:22 PM
Microhacker
Microhacker - avatar
+ 1
@Microhacker just for clarification. Computer cannot never ever give purely random number. It is always pseudo-random. If the computer would give number out of nothing, just when we ask it deliveres I'd agree that the number is random. But to generare (pseudo) random number we have to chose a seed just as you mentioned and create algorythm, which kinda defeats the point of randomness. BUT it doesn't mean that PRNG (Pseudo random number generators) are bad, or unuseful. The algorythms they use almost always guarantee different output from last one, which is what we want to achieve.
16th Oct 2017, 10:24 PM
Jakub Stasiak
Jakub Stasiak - avatar