20
How Random Is Random?
First, a prominent disclaimer is necessary. Most random data generated with Python is not fully random in the scientific sense of the word. Rather, it is pseudorandom: generated with a pseudorandom number generator (PRNG), which is essentially any algorithm for generating seemingly random but still reproducible data.
“True” random numbers can be generated by, you guessed it, a true random number generator (TRNG).
- Probably the most widely known tool for generating random data in Python is its random module, which uses the Mersenne Twister PRNG algorithm as its core generator.
Lets take an Example -
import random
for i in range (5):
print(random.randrange(1,10),end=" ")
Run-1 Output
1 8 7 5 1
Run-2 Output
5 3 3 6 9
But when we add an extra line to the code
import random
random.seed(5)
for i in range (5):
print(random.randrange(1,10),end=" ")
Run-1 Output
5 6 9 1 8
Run-2 Output
5 6 9 1 8
So, what is a seed.
A random seed is a number used to initialize a pseudorandom number generator.
For a seed to be used in a pseudorandom number generator, it does not need to be random. Because of the nature of number generating algorithms, so long as the original seed is ignored, the rest of the values that the algorithm generates will follow probability distribution in a pseudorandom manner.
In Python,
The default when you don’t seed the generator is to use your current system time or a “randomness source” from your OS if one is available.With random.seed(), you can make results reproducible, and the chain of calls after random.seed() will produce the same trail of data:
- This method measures some physical phenomenon that is expected to be random and then compensates for possible biases in the measurement process. Example sources include measuring atmospheric noise, thermal noise, and other external electromagnetic and quantum phenomena. For example, cosmic background radiation or radioactive decay as measured over short timescales represent sources of natural entropy.
So let's come back to the Python point of view How to generate truly Ranndom Numbers
There is a website called random.org
The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs
So first of all go the website and create an account and register yourselves an API Key
We Would be using requests for invoking the API
import requsts
import json
url = 'https://api.random.org/json-rpc/4/invoke'
raw_data = {
"jsonrpc": "2.0",
"method": "generateIntegers",
"params": {
"apiKey": 'your-api-key-here',
"n": 5,
"min": 1,
"max": 100,
"replacement": True
},
'id':1
}
headers = {
'Content-type': 'application/json',
'Content-Length': '200',
'Accept': 'application/json'
}
data=json.dumps(raw_data)
response = requests.post(
url=url,
data=data,
headers=headers
)
print(json.loads(response.text)['result']['random']['data'])
This will return a json object from which we can retrive the data
Output -
[56, 78, 47, 80, 3]
This API has many methods you can reference all methods here
20