Previous Page
Next Page

15.3. Random and Pseudorandom Numbers

The random module of the standard Python library generates pseudorandom numbers with various distributions. The underlying uniform pseudorandom generator uses the Mersenne Twister algorithm, with a period of length 2**19937-1. (Older versions of Python used the Whichmann-Hill algorithm, with a period of length 6,953,607,871,644.)

15.3.1.

15.3.1.1. Physically random and cryptographically strong random numbers

Pseudorandom numbers provided by module random, while very good, are not of cryptographic quality. If you want higher-quality random numbers (ideally, physically generated random numbers rather than algorithmically generated pseudorandom numbers), in Python 2.4, you can call os.urandom (from module os, not random).

urandom

urandom(n)

Returns n random bytes, read from physical sources of random bits such as /dev/urandom on recent Linux releases or from cryptographical-strength sources such as the CryptGenRandom API on Windows. If no suitable source exists on the current system, urandom raises NotImplementedError.


For an alternative source of physically random numbers, see http://www.fourmilab.ch/hotbits.

15.3.1.2. The random module

All functions of module random are methods of one hidden global instance of class random.Random. You can instantiate Random explicitly to get multiple generators that do not share state. Explicit instantiation is advisable if you require random numbers in multiple threads (threads are covered in Chapter 14). This section documents the most frequently used functions exposed by module random.

choice

choice(seq)

Returns a random item from nonempty sequence seq.

getrandbits

geTRandbits(k)

Returns a nonnegative long integer with k random bits, like choice(xrange(2**k)) (but much faster and with no problems for large k).

getstate

getstate( )

Returns a hashable, pickleable, and marshalable object S representing current state of the generator. You can later pass S to function setstate to restore the generator's state.

jumpahead

jumpahead(n)

Advances the generator state as if n random numbers had been generated. Computing the new state is faster than generating n random numbers would be.

random

random( )

Returns a random float r from a uniform distribution such that 0<=r<1.

randrange

randrange([start,]stop[,step])

Like choice(xrange(start,stop,step)), but much faster.

sample

sample(seq,k)

Returns a new list whose k items are unique items randomly drawn from seq. The list is in random order so that any slice is an equally valid random sample. seq may contain duplicate items. In this case, each occurrence of an item is a candidate for selection in the sample, and the sample may also contain duplicates.

seed

seed(x=None)

Initializes the generator state. x can be any hashable object. When x is None, and automatically when module random is first loaded, seed uses the current system time (or some platform-specific source of randomness, if any) to get a seed. x is normally a long integer up to 27814431486575L. Larger x values are accepted, but may produce the same generator states as smaller ones.

setstate

setstate(S)

Restores the generator state. S must be the result of a previous call to getstate (such a call may have occurred in another program, or in a previous run of this program, as long as object S has correctly been transmitted, or saved and restored).

shuffle

shuffle(alist)

Shuffles, in place, mutable sequence alist.

uniform

uniform(a,b)

Returns a random floating-point number r from a uniform distribution such that a<=r<b.


Module random also supplies functions that generate pseudorandom floating-point numbers from other probability distributions (Beta, Gamma, exponential, Gauss, Pareto, etc.) by internally calling random.random as their source of randomness.


Previous Page
Next Page