In Python Generating a Random Number to easily compare to other languages. For Generating a random number in python you need a random object and randint() function. You can get it from the inbuilt random module, so you have to import a Random module in the python application. In this tutorial, you will learn how to get Python random numbers and random functions with examples.
The syntax of using randint() function.
import random print(random.randint(start, end))
This returns a number RN in the inclusive range
start <= RN <= end, where the start and endpoints are included in the range.
Start and End – is range between you random number required. It should be an integer(number).
The return value is random between a range passed in randint() function in an integer.
Example Random numbers in python
Here is an example of a random number generator in python and print the result in the console.
import random print(random.randint(0, 4))
The functions of Python Random module
Here is some function of the random module with an example.
- Bookkeeping functions
- Functions for integers
- Functions for sequences
- Real-valued distributions
- Alternative Generator
This is groups name of python random modules, we will some of it and if you want to learn more go to official document mentioned in the link in the last.
random.choice(seq) – It will Return a random element from the non-empty sequence seq(tuple, list etc). If sequnce is empty, raises IndexError.
import random print(random.choice([1, 2, 3]))
random.randrange(start, stop[, step]) – With this you can add step to move (skip step) , to not slecting every number of step.
Check this example, step = 2. So start with 0 then 0 + step = 2, 3 + step =5 ..will not select as random number. Check output only print 0, 2 or 4.
import random print(random.randrange(0, 5, 2))
random.random()– Return the next random floating point number in the range [0.0, 1.0).
import random print(random.random())
random.shuffle(x[, random])– Shuffle the sequence x in place.
This is some function of the random module, in the official document have many. This all function you can use to do a Random number generator in python. It can be your python interview question, so check this below example and do comment if you have any questions suggestions.
QA: How to Generate a random number between given range.
Example 1: Python random number between 0 and 1
import random print(random.randint(0, 1))
Example 2: How to generate a Random number between 0 and 10 in Python.
import random print(random.randint(0, 10))
Example 3: How to get a Random number between 0 and 100.
import random print(random.randint(0, 100))
QA: Which and where random number used?
The Random number is used in games, lottery system and some application like select one winner participant.
Reference: https://docs.python.org/3/library/random.html (Official website – Visit this page to learn about all the)
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Examples of the Genrating random number in python made in Python 3, so it may change its different from python 2 or upgraded versions.
Warning: Do not use this pseudo-random generator for security purposes applications. For security or cryptographic uses, see the secrets module.