## Module `random`

## Official Python documentation

### Purpose

- Support the generation of sequences of random values. Must be imported for access to its capabilities.

### Basics

- Random value generators use a
*seed*to help start up their value generation process.

- The Python random value generator by default uses a seed based on the current time. Because time is always advancing, the different seed values from one program run to the next cause the generator to produced different random value sequences.

- When generating a random value from an interval, all the values in the interval are equally likely to be drawn (unless explicitly stated otherwise).

### Essential functions

`random()`

- Returns a random decimal number from the interval starting at 0 and up to but not including 1.0. In
math notation, that interval is written as [0, 1).

- Sample usage:
`random.random()`

Returns a random float value

`v`

such that 0.0 ≤`v`

< 1.0

`randrange( b )`

- Returns a random integer from the interval [ 0,
`b`

); that is, a base`b`

integer

- Sample usage:
`random.randrange( 10 )`

Returns an integer

`v`

such that 0 ≤`v`

< 10; that is, the base ten digits 0 through 9

`randrange( a, b )`

- Returns a random integer from the interval [
`a`

,`b`

)

- Sample usage:
`random.randrange( 31, 41 )`

Returns an integer

`v`

such that 31 ≤`v`

< 41.

`randrange( a, b, s )`

- Returns a random integer from the interval [
`a`

,`b`

) such that the integer equals`a`

`+`

`s`

`*`

`i`

for some integer`i`

.

- Sample usage:
`random.randrange( 1, 101, 2 )`

Returns a random odd number

`v`

between 1 and 100.

`choice( s )`

- Returns from a random value from the sequence
`s`

- Sample usage:
`random.choice( 'aeiou' )`

Returns a random vowel

- Sample usage:
`weekend = [ 'Saturday', 'Sunday' ] ; random.choice( weekend )`

Returns a random day of the weekend

`shuffle( s )`

- Randomly reorders the contents of
`s`

- Sample usage:
`rgb = [ 'Red', 'Green', 'Blue' ] ; random.shuffle( rgb )`

The contents of

`rgb`

are now in an unpredictable order

`seed( a )`

- Initializes the random number generator. Optional parameter
`a`

is used to configure the random number generator to a particular start state. This ability is useful during program delopment as it allows reproducibility during testing.

- Sample usage:
`random.seed()`

Uses the current time in the set up of the random number generator. As the current time is always changing, there should be different random numbers for each program run.

- Sample usage:
`random.seed( 1112 )`

Uses 1112 in the set up of the random number generator