random
module
In this lesson we are going to introduce random
module. It contains a random number generator (RNG), which you have probably used before if you play video games. The function random()
returns a random float number (decimal) between 0 and 1, excluding 1, which can also be written as 0 <= r < 1.
In order to use the random number generator, you need to import the random
module first:
import random
Then you can call the random()
function, which happens to have the same name as the module that contains it:
print(random.random())
Run the code a few times, and you'll get a few random numbers like these:
0.08827257404872013
0.6042273085605387
0.5651384727785271
0.12705473885429186
Random integers
The random
module also has some functions to generate random integers:
randint(a, b)
returns a random integer n such that a <= n <= b (end value 'b' included).
randrange(start, stop, step)
returns a randomly selected element from range(start, stop, step)
.
Here are some examples, including application with CodeCraft.
# randint()
print(random.randint(1, 10)) # 8 (integer from 1 to 10)
# demo in CodeCraft
h1 = random.randint(10, 20)
h2 = random.randint(10, 20)
h3 = random.randint(10, 20)
column(2, -20, h1, 'box_blue')
column(4, -20, h2, 'box_blue')
column(6, -20, h3, 'box_blue')
# randrange()
print(random.randrange(0, 51, 2)) # 18 (random even integer from 0 to 50)
# random integers from 1,3,5,7,9
n1 = random.randrange(1, 10, 2)
n2 = random.randrange(1, 10, 2)
n3 = random.randrange(1, 10, 2)
column(1, -20, n1, 'box_lime')
column(3, -20, n2, 'box_lime')
column(5, -20, n3, 'box_lime')
Check out the image, the blue columns have random heights from 10 to 20, and are located at x
positions of 2, 4, and 6. The green columns have odd number heights between 1 and 9, and they are located at x
positions of 1, 3, and 5.
Run the program a few times, and you'll notice that the blue columns change heights between 10 and 20 every time. The lime green columns have varying odd number heights between 1 and 9.
Functions for sequences
The random
module has some functions that are useful for manipulating sequences. Let's take a look:
random.choice(seq)
returns a random element from a non-empty sequence.random.shuffle(x)
shuffle the sequencex
in place.random.sample(population, k)
returns a list with lengthk
of unique elements chosen from thepopulation
sequence. This is used for random sampling without replacement.
Let's try the following examples:
random.shuffle()
# build columns to represent a number list
num = [1, 2, 3, 4, 5, 6, 7, 8]
for i in range(len(num)):
column(i+1, -20, num[i], 'quartz')
# shuffle()
random.shuffle(num)
print(num)
for i in range(len(num)):
column(-i - 1, -20, num[i], 'box_orange')
Console result:
[2, 8, 4, 7, 3, 6, 5, 1]
The white columns represent the original ordered list num
. Then num
list is shuffled in random order, and we build orange columns at the left negative side using the shuffled list. Now it's easy to see what shuffle()
does.
random.choice()
Next, let's use choice()
to choose a random element from a list and highlight it blue.
# diamond columns to indicate the original list num2
num2 = [2, 4, 5, 3, 8, 9, 10]
for i in range(len(num2)):
column(i, -15, num2[i], 'diamond')
# highlight a random choice item blue
c = random.choice(num2)
print(c)
column(num2.index(c), -15, c, 'linen_blue')
Run this code a few times. Every time, a random number in list num2
is printed to the console and the corresponding column is highlighted with blue. Your result should be similar to, but not the same as, the following picture.
Console result: 9
CodeCraft world result: the second last column is highlighted blue