Python Generate Random Number

Jul 25, 2019
import random

Random value between 0.0 - 1.0 (inclusive)

random.random()

Random Integer

random.randrange(0, 2)  # 0 or 1, will not get 2random.randrange(0, 10) # 0-9random.randrange(10)    # 0-9random.randrange(1, 10) # 1-9random.randrange(1, 1_000_000_000) # 1-999_999_999

Even number only

random.randrange(2, 10, 2)  # 2,4,6,8

Random from a list

items = [1, 7, 9, 12]random.choice(items)

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.