Skip to content

[Python] Use “random” Module To Generate Random Number

Recently, I have more and more tasks to do. Whether it is writing submitted papers, blogs, novels I want to write, applications I want to develop, various side projects on GitHub… with a long to-do list makes me often confused about what I should do now.

Everything is important, but when I am confused about which thing to do first, I tend to consume time. If you want to know, my average hesitation time is 20 minutes.

To solve my problem, I make a python script to randomly select the next task.

So by the way, record how to use the random module in python. This module is really convenient.


How to use random module

There are many use method in random module of python, but first we need to use the following code to import random module.

import random



Without importing, you can’t write any random number program.


Randomly generate a floating point number between 0-1

print(random.random())


Randomly generate an integer between 0-10

print(random.randint(0, 10))


Randomly take a value in the list (not necessarily a number)

elements = [1, 2, 3, 4]
print(random.choice(elements))


Shuffle

elements = [1, 2, 3, 4]
print(random.shuffle(elements))




References


Read More

Tags:

Leave a Reply