CS 3723
  Programming Languages  
  4. Randomization   


Random Numbers in Python: See random Module for more about Python's random number generator. First let's check out the generator:

Testing Python's Random Number Generator
#!/usr/bin/python
import sys # for sys.stdin.out
import random # for randint
def ranint(n): # random ints in a range
    for j in range(n): # repeat n times
        sys.stdout.write("TEST randint(3,8): ")
        for i in range(40):
            sys.stdout.write( str(random.randint(3,8)))
        sys.stdout.write("\n")
    
def unif(n): # uniform doubles in [0,1)
    sys.stdout.write("TEST random(): ")
    for i in range(n): # repeat n times
        r = random.random()
        sys.stdout.write(("%.16f" % r) + " ")
    sys.stdout.write("\n")

ranint(3)

random.seed(3141592653589793238462643383279)
unif(3)
random.seed(3141592653589793238462643383279)
unif(3)
random.seed(3141592653589793238462643383278)
unif(3)
Output
TEST randint(3,8): 6477777536557765688667857735353588337333
TEST randint(3,8): 5457383663443338745555658658475367738746
TEST randint(3,8): 4866645868473845633873355666755673684438
TEST random(): 0.7368353669331363 0.8067286494837592 0.4544903984116798 
TEST random(): 0.7368353669331363 0.8067286494837592 0.4544903984116798 
TEST random(): 0.5725893213877952 0.4347496651527191 0.8911326623558873 

Items of Interest or for study:

  • See random Module for more about random numbers in Python.
  • The bottom-level function is random() which returns uniformly distributed doubles r in the range 0.0 <= r < 1.0. This generator won't repeat until after generating lim = 219937 - 1 numbers. (The number lim is itself a ridiculously large number with over 6000 digits, since log10lim = (log102)*19937 = 0.302029995664*19937 = 6001.63502355.)
  • You can seed this generator with seed(x), where x can be anything "hashable".


List of Students in the Class in Random Order: In order to call on random students, I wanted to randomize the order of the class list.

Next comes a program that will randomize the order of items in a list. For testing, I first used 6 students from the class with names starting with successive letters of the alphabet. The program reads the names from a file, puts them in a list, randomizes the order, and prints the list. I show several runs. (The last run left the file unchanged -- one chance in 720 of that happening. Well, I did 10 runs, so more like one chance in 72.)

Randomizing Program Output
#!/usr/bin/python
import sys # for sys.stdin.out
import random # for randint
# read in students' names
f = open("students6.txt",'r')
s = [] # empty list
for line in f:
    s.append(line) # tack next item on
    
sys.stdout.write("STUDENTS:\n")
for line in s:
    sys.stdout.write(line)

sys.stdout.write("\nRANDOMIZING:\n")
d = len(s)
for i in range(0,d-1): # not incl. d-1
    r = random.randint(i,d-1) #incl. d-1
    s[i], s[r] = s[r], s[i]
    sys.stdout.write(("%2i" % i) +
          "<->"+("%2i" % r)+",")
    if i%10 ==9:
        sys.stdout.write("\n")
sys.stdout.write("\n")
    
sys.stdout.write("\nRESULT:\n")
for i in range(0,d):
    sys.stdout.write(("%2i" % i) +
          " " + s[i])
% cat students6.txt
Andrada, Vicente
Barnes, Daniel
Cotner, Bascom
Deshaies, Derek
Evans, Ryan
Franco, Monica

% python students.py
STUDENTS:
Andrada, Vicente
Barnes, Daniel
Cotner, Bascom
Deshaies, Derek
Evans, Ryan
Franco, Monica

RANDOMIZING:
 0<-> 2, 1<-> 3, 2<-> 3, 3<-> 3, 4<-> 5,

RESULT:
 0 Cotner, Bascom
 1 Deshaies, Derek
 2 Barnes, Daniel
 3 Andrada, Vicente
 4 Franco, Monica
 5 Evans, Ryan
More Runs
RANDOMIZING:
 0<-> 4, 1<-> 1, 2<-> 3, 3<-> 3, 4<-> 4,

RESULT:
 0 Evans, Ryan
 1 Barnes, Daniel
 2 Deshaies, Derek
 3 Cotner, Bascom
 4 Andrada, Vicente
 5 Franco, Monica

RANDOMIZING: 0<-> 1, 1<-> 5, 2<-> 2, 3<-> 3, 4<-> 4, RESULT: 0 Barnes, Daniel 1 Franco, Monica 2 Cotner, Bascom 3 Deshaies, Derek 4 Evans, Ryan 5 Andrada, Vicente
RANDOMIZING:
 0<-> 3, 1<-> 4, 2<-> 2, 3<-> 5, 4<-> 5,

RESULT:
 0 Deshaies, Derek
 1 Evans, Ryan
 2 Cotner, Bascom
 3 Franco, Monica
 4 Andrada, Vicente
 5 Barnes, Daniel

RANDOMIZING: 0<-> 0, 1<-> 1, 2<-> 2, 3<-> 3, 4<-> 4, RESULT: 0 Andrada, Vicente 1 Barnes, Daniel 2 Cotner, Bascom 3 Deshaies, Derek 4 Evans, Ryan 5 Franco, Monica

Here is the result of running the same program 3 times with input file the list of all 48 students. Check out how the two Aarons in the class are clustered. Chance or something sinister?

Input Results of Three Runs
STUDENTS:
Abril, Evanilson
Alderson, Cameron
Andrada, Vicente
Aquino, Laura
Barnes, Daniel
Bhupathiraju, So.
Blancas, Marco
Cotner, Bascom
Deshaies, Derek
Evans, Ryan
Franco, Monica
Gallagher, Sean
Gomez, Gregory
Gonzales, Aaron
Gonzales, Marcos
Gonzales, Raul
Goode, Mackenzie
Guerra, Daniel
Honore-Armour, W.
Huerta, Aaron
Iturbe, Gabriel
Jayagopan, Karth.
Johnson, Gary
Johnson, Justin
Jones, Joseph
Jordan, Matthew
Jordan, Rodney
La Valle, Chris
Lewis, Aaron
Little, Kurt
Lopez, Marvin
Lorenzana, Marcus
Martinez, Adrian
McKinney, Jeffery
Mireles, Jose
Muraj, Haseeb
Navarro, Armando
Pagano, Jacob
Pradhan, Anjan
Price, Michael
Raveendran, Deepa
Rutherford, Ryan
Salazar, Zachary
Seng, Sokhun
Smith, Joshua
Stapleton, George
Villarreal, Josh
Watson, Kyle
RESULT:
 0 Jordan, Matthew
 1 Franco, Monica
 2 Rutherford, Ryan
 3 Stapleton, George
 4 Gallagher, Sean
 5 Mireles, Jose
 6 Jordan, Rodney
 7 Abril, Evanilson
 8 Huerta, Aaron
 9 Johnson, Gary
10 Barnes, Daniel
11 Bhupathiraju, So.
12 Salazar, Zachary
13 Deshaies, Derek
14 McKinney, Jeffery
15 Gonzales, Marcos
16 Evans, Ryan
17 Raveendran, Deepa
18 Price, Michael
19 Aquino, Laura
20 Honore-Armour, W.
21 Lewis, Aaron
22 Martinez, Adrian
23 Seng, Sokhun
24 Gomez, Gregory
25 Iturbe, Gabriel
26 Gonzales, Aaron
27 Lorenzana, Marcus
28 Jayagopan, Karth.
29 Guerra, Daniel
30 Blancas, Marco
31 Little, Kurt
32 Watson, Kyle
33 Navarro, Armando
34 Cotner, Bascom
35 Alderson, Cameron
36 Jones, Joseph
37 Muraj, Haseeb
38 Lopez, Marvin
39 Smith, Joshua
40 Gonzales, Raul
41 La Valle, Chris
42 Pagano, Jacob
43 Andrada, Vicente
44 Johnson, Justin
45 Pradhan, Anjan
46 Goode, Mackenzie
47 Villarreal, Josh
RESULT:
 0 Martinez, Adrian
 1 Gonzales, Marcos
 2 Andrada, Vicente
 3 Watson, Kyle
 4 Alderson, Cameron
 5 Abril, Evanilson
 6 Blancas, Marco
 7 Lorenzana, Marcus
 8 Bhupathiraju, So.
 9 Huerta, Aaron
10 Smith, Joshua
11 Jones, Joseph
12 Gallagher, Sean
13 Goode, Mackenzie
14 Cotner, Bascom
15 Stapleton, George
16 Navarro, Armando
17 Iturbe, Gabriel
18 Lopez, Marvin
19 Jordan, Rodney
20 Little, Kurt
21 Rutherford, Ryan
22 Pagano, Jacob
23 Deshaies, Derek
24 Gonzales, Aaron
25 Evans, Ryan
26 Raveendran, Deepa
27 Muraj, Haseeb
28 Gonzales, Raul
29 Pradhan, Anjan
30 Jordan, Matthew
31 La Valle, Chris
32 Franco, Monica
33 Jayagopan, Karth.
34 Gomez, Gregory
35 Lewis, Aaron
36 Price, Michael
37 Mireles, Jose
38 Honore-Armour, W.
39 Johnson, Gary
40 Guerra, Daniel
41 McKinney, Jeffery
42 Villarreal, Josh
43 Aquino, Laura
44 Barnes, Daniel
45 Seng, Sokhun
46 Salazar, Zachary
47 Johnson, Justin
RESULT:
 0 Gallagher, Sean
 1 Salazar, Zachary
 2 Mireles, Jose
 3 Jordan, Matthew
 4 Andrada, Vicente
 5 McKinney, Jeffery
 6 Lopez, Marvin
 7 Muraj, Haseeb
 8 Evans, Ryan
 9 Seng, Sokhun
10 Huerta, Aaron
11 Jordan, Rodney
12 Martinez, Adrian
13 Rutherford, Ryan
14 Franco, Monica
15 Johnson, Gary
16 Stapleton, George
17 Johnson, Justin
18 Navarro, Armando
19 Bhupathiraju, So.
20 Honore-Armour, W.
21 Barnes, Daniel
22 Little, Kurt
23 Price, Michael
24 Guerra, Daniel
25 Gonzales, Aaron
26 Gonzales, Raul
27 Deshaies, Derek
28 La Valle, Chris
29 Cotner, Bascom
30 Abril, Evanilson
31 Watson, Kyle
32 Aquino, Laura
33 Raveendran, Deepa
34 Smith, Joshua
35 Villarreal, Josh
36 Alderson, Cameron
37 Lorenzana, Marcus
38 Lewis, Aaron
39 Jayagopan, Karth.
40 Iturbe, Gabriel
41 Goode, Mackenzie
42 Jones, Joseph
43 Pradhan, Anjan
44 Blancas, Marco
45 Gomez, Gregory
46 Pagano, Jacob
47 Gonzales, Marcos
Data for First Randomization (Interchanges made)
RANDOMIZING:
 0<->25, 1<->10, 2<->41, 3<->45, 4<->11, 5<->34, 6<->26, 7<->25, 8<->19, 9<->22,
10<->11,11<->34,12<->42,13<->19,14<->33,15<->33,16<->22,17<->40,18<->39,19<->45,
20<->39,21<->28,22<->32,23<->43,24<->42,25<->39,26<->45,27<->31,28<->28,29<->40,
30<->45,31<->40,32<->47,33<->36,34<->39,35<->39,36<->42,37<->39,38<->45,39<->44,
40<->42,41<->42,42<->44,43<->44,44<->44,45<->45,46<->47,

Items of Interest or for study:

  • The algorithm is explained a little better at: Randomization Algorithm.

  • For opening a file, 'r' specifies reading, 'w' specifies writing, 'a' for append, and there are many other options.

  • The code above iterates over the lines in the file. For another option that explicitly checks for EOF you could use:

    Reading a File
    f = open("foo",'r')
    while True:
        line = f.readline();
        if not line: # at EOF return empty string
            break
    

    See Python files for much more about files than you want to know.

(Revision date: 2014-05-24. Please use ISO 8601, the International Standard.)