Python is one of the programming languages and it defines a set of functions whose use takes place for the generation or manipulation of random numbers via the random module in python. Furthermore, the reliance of the functions in the random module is on a pseudo-random number generator function random(). Moreover, randint() refers to an inbuilt function belonging to the random module that is in Python3.
Random Module in Python
random() Function in Python
random(), simply speaking, is an inbuilt function of the random module in Python3 and it helps in the derivation of the python random list. Furthermore, the random module in Python provides access to some useful functions and one of them is able to facilitate the generation of random floating numbers, which is random(). Also, its syntax is random. random() python.
Syntax: random.random()
Parameters: Any parameter is not accepted by this method.
Returns: Random floating number between 0 and 1 is returned by this method.
First of all, example #1: Simple implementation of sample() function
# Python3 program to demonstrate
# the use of random() function .
# import random
from random import random
# Prints random item
Moreover, print(random())
Furthermore, output:
0.41941790721207284
Moreover, Example 2:
# Python3 program to demonstrate
# the use of random() function .
# import random
Also, from random import random
lst = []
for i in range(10):
lst.append(random())
# Prints random items
Furthermore, print(lst)
Finally, Output:
[0.12144204979175777, 0.27614050014306335, 0.8217122381411321, 0.34259785168486445, 0.6119383347065234, 0.8527573184278889, 0.9741465121560601, 0.21663626227016142, 0.9381166706029976, 0.2785298315133211]
Browse more Topics Under Functions
randint() Function in Python
randint() refers to an inbuilt function belonging to the random module in Python3. Furthermore, access is provided by random module provides access to some beneficial functions. Moreover, one of the functions is to generate random numbers, which is randint().
Syntax :
randint(start, end)
Parameters :
(start, end) : Both of them have to be integer-type values.
Returns :
A random integer in the range [start, end] consisting of the endpoints.
Errors and Exceptions :
ValueError: Returns a ValueError when floating-point values are passed as parameters.
TypeError: TypeError is returned when anything other than
numeric values is passed as parameters.
Code #1 :
# Python3 program explaining work
# of randint() function
# imports random module
import random
# Generates a random number between
# a given positive range
r1 = random.randint(0, 10)
Furthermore, another important point, print("Random number between 0 and 10 is % s" % (r1))
# Generates a random number between
# two given negative range
r2 = random.randint(-10, -1)
Also, another important point, print("Random number between -10 and -1 is % d" % (r2))
# Generates a random number between
# a positive and a negative range
r3 = random.randint(-5, 5)
Furthermore, another important point, print("Random number existing between -5 and 5 is % d" % (r3))
So, output:
Random number between 0 and 10 is 5
Also, Random number between -10 and -1 is -7
Finally, Random number between -5 and 5 is 2
Furthermore, code #2 : Program demonstrating the ValueError.
# imports random module
import random
'''If we pass floating point values as
parameters in the randint() function'''
r1 = random.randint(1.23, 9.34)
Moreover, print(r1)
So, output :
Traceback (most recent call last):
File "/home/f813370b9ea61dd5d55d7dadc8ed5171.py", line 6, in
r1=random.randint(1.23, 9.34)
File "/usr/lib/python3.5/random.py", line 218, in randint
return self.randrange(a, b+1)
File "/usr/lib/python3.5/random.py", line 182, in randrange
raise ValueError("non-integer arg 1 for randrange()")
ValueError: non-integer arg 1 for randrange()
FAQs on Using Random and Randint Functions
Question 1: Explain the working of randint in a simple manner?
Answer 1: randint(1,101) will facilitate the automatic selection of a random integer between 1 and 100 for you. Furthermore, the process is fairly simple. Moreover, a random number will be generated between 1 and 20 by this code, and that number will be multiplied by 5.
Question 2: What is meant by the random module in python?
Answer 2: The random module in python is a built-in module that leads to the generation of pseudo-random variables. Furthermore, its use can take place to perform some action randomly such as to randomly shuffle elements, selecting random elements from a list, getting a random number, etc.