Random Numbers

The random module can be used to make random numbers in Python. The function random() generates a number between 0 and 1.

Online Courses
Complete Python Bootcamp: Go from zero to hero in Python

Generate random nubers

Generate a real number between 0 and 1
Simply call the random() method to generate a real (float) number between 0 and 1.

Generate a number between 0 and 50
We use the randint() method to generate a whole number.

Generate a random number between 1 and 10
Change the parameters of randint() to generate a number between 1 and 10.

List of random numbers

To generate a list of 100 random numbers:

Choosing random items from a list

To get 3 random items from a list:

Previous Post
Next Post

Leave a Reply