+ 3
How does the "randomness" in computer comes? Or why won't we expect computers to do a wrong calculation?
Functions or modules providing randomness are really useful in programming. I'm curious about how it works. I tried to read some Python modules such as "random", "_random" , or urandom in "os". Unsurprisingly, I'm more confused now. I thought of terms like degrees of freedom or entropy. Practically, does anyone know how computers eliminate random error during calculation and how they regenerate it back when we are using random functions? Any idea or related article may help. Thx.
2 Antworten
+ 3
It is great that you are trying to understand what is under the hood. Theoretically, nothing is "absolutely" random, but might differ in terms of predictability.
The idea of random number generation comes from here:
If we had a factor(number) that is constantly changing, we can divide that number by 10, for example, and the remainder in this division could be called a "random" number between 0 and 10.
The factor that is used in all programming languages is "time". All computers come with a really precise clock that are high-precision enough to be used as the factor I mentioned above. And yes, it is precise enough to be considered random everytime you get the time in computer software level.
A python example to generate 20 random numbers between 0 and 10:
import time
def rand(start,end):
# get current precise time
currentPreciseTime=time.time()*1000000
# divide it by length of range given and get the remainder
# and add it the start position to convert length of range
# to real range we need
randomValue=start+currentPreciseTime%(end-start)
# you would probably want to round it so that you could get an
# integer value in that range
return round(randomValue)
print("Here comes 20 new random numbers between 0 and 10!")
for i in range(0,20):
print(rand(0,10))
+ 2
Wow! Thank you, Moshen. This method sounds really clever. Much simpler than my old idea about judging the direction of alternative current, and use some recursion.