The best page to pretend you're working

A Blog written by Zanidd

There’s an easy way to generate random numbers with Python:

The Python `random`

module provides (pseudo-) random number generators. So just `import random`

.

To generate a random number use

`random.random()`

By default you get a random number between 0 and 1.0. If you want a random number in a specified range or from a sequence you can do it in a couple of ways:

Click on the links above to jump to the method or read through the whole article.

`random.randint(1, 50)`

Generates a random integer between 1 and 50.

`random.uniform(1, 50)`

Generates a random number with floats.

`random.randrange(0, 200, 5)`

will return a random integer between 0 and 200. The int provided will be always in 5 steps, meaning you get 10, 45, 20, 5, etc…

`t = (5, 23, 768, 4, 1028)`

`random.choice(t)`

will return one of the elements in `t`

%d bloggers like this: