Random module

In this lesson, we are going to introduce random module, it has some frequently used functions to generate random numbers or manage numbers.

Almost all module functions depend on the basic function random(), which returns a random float number r from zero to one. (0 <= r <= 1)

First, check the very top of the program, make sure you have this line:

import random

Then let's continue:

print( random.random() )

Run the above lines a few times, you'll get a few random float like these:

0.08827257404872013
0.6042273085605387
0.5651384727785271
0.12705473885429186

Functions for integers

random module has some functions for integers:

randint(a, b), returns a random integer n such that a <= n <= b (end point included)

randrange(start, stop[, step]), returns a randomly selected element from range(start, stop, step).

Let's continue with the above code,

# 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, 39)              # blue
column(4,-20, h2, 39)          
column(6,-20, h3, 39)          

# 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, 45)          # lime green
column(3,-20, n2, 45)
column(5,-20, n3, 45)

Check out the image, the blue columns has random height from 10 to 20, they locates at x=2,4,6; Other green columns have odd number heights (1 to 9) and they locates at x=1,3,5.

Run the program a few times, you'll notice that the blue columns change heights within 10 to 20, the lime green columns have varying odd number heights from 1 to 9.

Functions for sequences

random module has some functions useful to manipulate sequence. Le't take a look:

  • random.choice(seq), returns a random element from the non-empty sequence.

  • random.shuffle(x), shuffle the sequence x in place.

  • random.sample(population, k) returns a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.

You can comment out all the above examples, reload to clear the game screen.

Let's try the following examples:

# build columns to represent a number list
num = [1,2,3,4,5,6,7,8]
for i in range( len(num)+1 ):
    column(i, -20, num[i], 35)      # white

# shuffle()
random.shuffle(num)
for i in range( len(num)+1 ):
    column(i, -15, num[i], 47)       # orange

The white columns represent the original ordered list num; then num is shuffled into random order, and we build orange columns at the left negative side indicating the shuffled list. It's so obvious everyone can see what shuffle() does.

(pic)

Next, let's continue to try choice() to choose a random element from a list, highlighted with color blue

# diamond columns indicating the original list num2
num2 = [2,4,5,3,8,9,10]
for i in range( len(num2) ):
  column(i, -15, num2[i], 3)       # diamond columns

# highlight a random choice item blue
c = random.choice(num2)
print(c)
column( num2.index(c), -15, c, 62)    #'wool_blue'

Run the above code a few times, each time a random number in list num2 is printed in the console, and the corresponding column is highlighted blue color, such as one result showed in the picture:

Console result: 9 CodeCraft world result: the second last column is highlighted blue:

=== end here??

# sample() 
num_spl = random.sample(num, 5)   # randomly choose 5 elements to make a new list, leave the original list unchanged
print( num_spl )    # [4,1,5,7,3]

for i in range( len(num_spl) ):
    column(i, -12, num_spl[i], 'wool_red')    # the chosen numbers, red columns in the front row

results matching ""

    No results matching ""