When we talk about random numbers and probability, we usually mean a number between 0.0 to <1.0.
0.0 is the event never happening, and 0.999 is the event happening.
Generating random numbers with a computer is complex. Programming languages usually have a library function which will seed the random number generator for you. Python handles this with the random module.
Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.
The Mersenne Twister is a pseudorandom number generator (PRNG). It is by far the most widely used general-purpose PRNG
and
A pseudorandom number generator (PRNG), also known as a deterministic random bit generator (DRBG), is an algorithm for generating a sequence of numbers whose properties approximate the properties of sequences of random numbers. The PRNG-generated sequence is not truly random, because it is completely determined by a relatively small set of initial values, called the PRNG's seed (which may include truly random values). Although sequences that are closer to truly random can be generated using hardware random number generators, pseudorandom number generators are important in practice for their speed in number generation and their reproducibility.
Python, like all programming languages will generate random numbers.
import random
print(random.random())
That might give you a number like:
0.10940338745940381
or
0.7921996495976178
or
0.5752671510235025
or
0.04780427949110977
or
0.9540278243078905
or
0.22425650016401177
or
0.866212373677892
or
0.683149562597131
or
0.7007903302436131
or perhaps something truly useful like,
8.010696446425492e-05
As you can see, the random() will only generate a number between 0.0 and 1.0. These numbers don't fit on the face of a die, nor do they help you when you want to play a game of 'guess a number from 1 to 10'.
import random
for i in range(1,11):
print(i, random.randint(1,10), random.random())
My output and your output should be different. Here's mine:
1 3 0.24326554564589864
2 1 0.18308787873100463
3 5 0.7207806043918445
4 9 0.0973342135948797
5 9 0.593122826395729
6 5 0.3597623960977868
7 9 0.8817712957458808
8 7 0.5897880781225155
9 5 0.09039521721782362
10 10 0.5453265930448745
What are we looking at? The first column is the iteration of the loop. The second column is the random number that Python generated. It was generated by using random.randint(). The third column is the random number generated by random.random().
Clearly, a number generated by random.randint() will fit on a face of a die and work well in a game of 'guess a number from 1 to 10'.
import random
for i in range(1,11):
print(random.randint(1,10))
This was my output, what was yours?
5
7
1
3
4
10
3
8
8
4
>>>
Python has some interesting functions built into the random module. My favourites are random.choice(), random.shuffle() and random.sample().
import random
print(random.choice('abcdefghijklmnopqrstuvwxyz'))
OUTPUT: h
Remember that list called caf_specials that we used in the loop unit? I told you not to worry about what a list was. Let's use the list again in my shuffle() example, and continue to not worry about it.
import random
caf_specials = [ 'breakfast sandwich', 'milk', 'rice', 'pizza', 'salad', 'fish and chips' ]
random.shuffle(caf_specials)
print(caf_specials)
OUTPUT: ['salad', 'pizza', 'milk', 'fish and chips', 'breakfast sandwich', 'rice']
Let's reuse the caf_specials list, one more time. Don't worry about how it works!
import random
caf_specials = [ 'breakfast sandwich', 'milk', 'rice', 'pizza', 'salad', 'fish and chips' ]
print (random.sample(caf_specials, 3))
OUTPUT: ['rice', 'fish and chips', 'salad']
In my examples I am just printing the random number. How should we hold the value?
If you are playing a game that uses 2 6-sided dice, does it make more sense to generate a random number from 2 to 12 or 1 to 6 + 1 to 6?
Look at various dice online. What is the difference between a 1d12 and 1d6 + 1d6? How is the 1d10 used to generate 1 - 10 and 0 -100?
When asked to generate a percentage chance, how can we use the numbers 1 - 10 to represent percentages?