Main.RandomNumbers History

Hide minor edits - Show changes to markup

April 05, 2010, at 11:29 AM MST by 10.84.44.76 -
Added lines 1-39:

(:source lang=python:)

"""Random numbers in python http://docs.python.org/library/random.html """

  1. First we need to import the random module:

import random

  1. We can set the seed for the random number generator by:

random.seed(1)

  1. this ensures that the series of number that are generated is
  2. reproducible
  3. get a random number between 0 and 9:

print random.randrange(0, 10)

a = range(0, 100)

  1. shuffle the elements of the list:

random.suffle(a)

print random.uniform(0, 1)

  1. Let's create an instance of the Random class:

rand = random.Random(10)

  1. The argument we gave the constructor is the seed
  2. now you can use all the functions in the random module as methods
  3. of the instance we created:

print rand.randrange(0, 10) a = range(0, 100)

  1. shuffle the elements of the list:

rand.suffle(a) print rand.uniform(0, 1)

(:sourceend:)