0% found this document useful (0 votes)
6 views3 pages

Generating Random Number List in Python

The document explains how to generate random numbers in Python using the random module. It covers methods for generating a single random number, random integers within a range, and lists of random numbers using loops and the sample() method. Additionally, it describes functions for generating floating-point numbers and manipulating sequences, such as choice(), choices(), and shuffle().

Uploaded by

arka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Generating Random Number List in Python

The document explains how to generate random numbers in Python using the random module. It covers methods for generating a single random number, random integers within a range, and lists of random numbers using loops and the sample() method. Additionally, it describes functions for generating floating-point numbers and manipulating sequences, such as choice(), choices(), and shuffle().

Uploaded by

arka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

GENERATING RANDOM NUMBER LIST IN PYTHON

There is a need to generate random numbers when studying a model or behavior of a program for
different range of values. Python can generate such random numbers by using the random
module. In the below examples we will first see how to generate a single random number and
then extend it to generate a list of random numbers.
 Generating a Single Random Number
The random() method in random module generates a float number between 0 and 1.
import random
n = random.random()
print(n)

Output
0.2112200

 Generating Number in a Range


The randint() method generates a integer between a given range of numbers.
import random
n = random.randint(0,22)
print(n)

Output
2

 Generating a List of numbers Using For Loop


We can use the above randint() method along with a for loop to generate a list of numbers. We
first create an empty list and then append the random numbers generated to the empty list one by
one.
import random
randomlist = []
for i in range(0,5):
n = random.randint(1,30)
randomlist. append(n)
print(randomlist)

Output
[10, 5, 21, 1, 17]
 Using random.sample()
We can also use the sample() method available in random module to directly generate a list of
random numbers.Here we specify a range and give how many random numbers we need to
generate.
import random
#Generate 5 random numbers between 10 and 30
randomlist = random. sample (range(10, 30), 5)
print(randomlist)
Output
[16, 19, 13, 18, 15]

Following functions deal with floating point random numbers

 random.random() − This function randomly generates a floating point number between


0.0 and 1.0

>>> random.random()
0.668544544081956

 random.uniform() − This function returns a floating point random number between two
parameters.

>>> random.uniform(0.5,1.5)
1.2760281470664903
>>> random.uniform(1,10)
7.336985794193224
>>> random.uniform(10,5)
7.817794757786727

Following functions act upon sequence objects viz. string, list or tuple

 random. choice() − This function picks a random element from the sequence. If the
sequence is empty, IndexError is thrown.

>>> random.choice("Tutorialspoint")
'o'
>>> random.choice(range(10))
2
>>> random.choice([15,31,6,29,55, 5])
55
>>> random.choice((15,31,6,29,25, 55))
15
 random.choices() − This function chooses multiple elements from a list in random
manner. First parameter to this function is the sequence and second parameter is the
number of choices to be made.

>>> name = "TutorialsPoint"


>>> random.choices(name, k = 2)
['T', 'o']

 random.shuffle() − This function reorders elements in a mutable sequence and places


them randomly.

>>> num = [10,20,30,40,50]


>>> random.shuffle (num)
>>> num
[50, 20, 40, 30, 10]

Example: This function shuffles the list and randomly arranges them.

list = [1, 3, 5, 10, 4]


print (" list before shuffling: ", end="")
for j in range (0, len (list)):
print (list[j], end=" ")
print("\r")
random.shuffle (list)
print ("list after shuffling: ", end="")
for j in range(0, len(list)):
print (list[j], end=" ")
print("\r")

Output:

list before shuffling : 1 3 5 10 4


list after shuffling : 3 10 1 4 5

Source: https://www.tutorialspoint.com/random-numbers-in-python

You might also like