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, like all programming languages will generate random numbers.
import randomprint(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 randomfor 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 randomfor i in range(1,11): print(random.randint(1,10))This was my output, what was yours?
57134103884>>>Python has some interesting functions built into the random module. My favourites are random.choice(), random.shuffle() and random.sample().
import randomprint(random.choice('abcdefghijklmnopqrstuvwxyz'))OUTPUT: hRemember 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 randomcaf_specials = [ 'breakfast sandwich', 'milk', 'rice', 'pizza', 'salad', 'fish and chips' ]print (random.sample(caf_specials, 3))OUTPUT: ['rice', 'fish and chips', 'salad']import turtle #import the turtle moduleimport random #import the random modulemyColours = ['orange', 'yellow', 'green', 'blue', 'black', 'purple', 'red'] #a list of stringsbob = turtle.Turtle() #create a turtle named bobbob.goto(55, 75) #move bob to x55 and y75#let's add some random valuesbob.goto(random.randint(0,255), random.randint(0,255)) #generate a random x and yfor x in range(0,5): bob.goto(random.randint(0,255), random.randint(0,255)) #generate a random x and y 4 timesfor x in range(0,15): bob.goto(random.randint(0,255), random.randint(0,255)) #generate a random x and y each time the loop happens randomColour= random.choice(myColours) #use random.choice to make a random selection from myColours list bob.pencolor(randomColour) #set the pen to that random myColours
import turtle #import the turtle moduleimport random #import the random modulemyColours = ['orange', 'yellow', 'green', 'blue', 'black', 'purple', 'red'] #a list of stringsbob = turtle.Turtle() #create a turtle named bobfor x in range(0,200): bob.penup() bob.goto(random.randint(-255,255), random.randint(-255,255)) bob.pendown() randomColour= random.choice(myColours) bob.pencolor(randomColour) bob.dot() #try it like this, then comment this line and uncomment the next line #bob.dot(random.randint(1,100)) #what happens to the dot size?