Wednesday, March 13, 2013

The random Module (python)

The random module is pseudo-random number generator.  This means that's truly not random, but close enough.

You should seed, initialize the generator with either a value, or with the system time (default value).

Example:

>>> import random
>>> random.seed()
>>> print random.random()
0.884447776659
>>> print random.random()
0.379767969805
>>> print random.random()
0.580327390006

Random also has the ability to pick from ranges and collection of elements, like letter:

>>> random.randrange(start=0, stop=101, step=1)
52
>>> random.choice('abcdefghij')
'b'


Happy Randomness.


No comments: