Main.RandomNumbers History
Hide minor edits - Show changes to markup
April 05, 2010, at 11:29 AM MST
by -
Added lines 1-39:
(:source lang=python:)
"""Random numbers in python http://docs.python.org/library/random.html """
- First we need to import the random module:
import random
- We can set the seed for the random number generator by:
random.seed(1)
- this ensures that the series of number that are generated is
- reproducible
- get a random number between 0 and 9:
print random.randrange(0, 10)
a = range(0, 100)
- shuffle the elements of the list:
random.suffle(a)
print random.uniform(0, 1)
- Let's create an instance of the Random class:
rand = random.Random(10)
- The argument we gave the constructor is the seed
- now you can use all the functions in the random module as methods
- of the instance we created:
print rand.randrange(0, 10) a = range(0, 100)
- shuffle the elements of the list:
rand.suffle(a) print rand.uniform(0, 1)
(:sourceend:)
