How do you randomize a number in python?

To generate random number in Python, randint() function is used. This function is defined in random module.


Source Code

# Program to generate a random number between 0 and 9

# importing the random module
import random

print(random.randint(0,9))

Output

5

Note that we may get different output because this program generates random number in range 0 and 9. The syntax of this function is:

random.randint(a,b)

This returns a number N in the inclusive range [a,b], meaning a <= N <= b, where the endpoints are included in the range.


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.

Example

import random
n = random.random()
print(n)

Output

Running the above code gives us the following result −

0.2112200

Generating Number in a Range

The randint() method generates a integer between a given range of numbers.

Example

import random
n = random.randint(0,22)
print(n)

Output

Running the above code gives us the following result −

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.

Example

import random
randomlist = []
for i in range(0,5):
n = random.randint(1,30)
randomlist.append(n)
print(randomlist)

Output

Running the above code gives us the following result −

[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.

Example

import random
#Generate 5 random numbers between 10 and 30
randomlist = random.sample(range(10, 30), 5)
print(randomlist)

Output

Running the above code gives us the following result −

[16, 19, 13, 18, 15]

How do you randomize a number in python?

Updated on 08-Aug-2019 06:54:57

  • Related Questions & Answers
  • Generating Random Prime Number in JavaScript
  • Generating random number in a range in C
  • Generating random Id’s in Python
  • Generating random numbers in Java
  • Generating random numbers in C#
  • Generating Random String Using PHP
  • Generating Random id's using UUID in Python
  • Generating random hex color in JavaScript
  • Generating Random Short Id in Node.js
  • Generating a random number that is divisible by n in JavaScript
  • Generating random string of specified length in JavaScript
  • Generating random strings until a given string is generated using Python
  • Random Number Functions in Python
  • Generating n random numbers between a range - JavaScript
  • Generating random string with a specific length in JavaScript

Python defines a set of functions that are used to generate or manipulate random numbers through the random module. 

Functions in the random module rely on a pseudo-random number generator function random(), which generates a random float number between 0.0 and 1.0. These particular type of functions is used in a lot of games, lotteries, or any application requiring a random number generation.

Example of Randon Number in Python

Generating random number list in Python using random().

Python3

import random

num = random.random()

print(num)

Output: 

0.30078080420602904

Different ways to Generate a Random Number in Python

Method 1: Generating random number list in Python choice()

 The choice() is an inbuilt function in the Python programming language that returns a random item from a list, tuple, or string.

Python3

import random

list1 = [1, 2, 3, 4, 5, 6]

print(random.choice(list1))

string = "striver"

print(random.choice(string))

Output:

5
t

Method 2: Generating random number list in Python randrange(beg, end, step)

The random module offers a function that can generate random numbers from a specified range and also allows room for steps to be included, called randrange().

Python3

import random

print("A random number from list is : ", end="")

print(random.choice([1, 4, 8, 10, 3]))

print("A random number from range is : ", end="")

print(random.randrange(20, 50, 3))

Output: 

A random number from list is : 4
A random number from range is : 41

Method 3: Generating random number list in Python using seed()

The seed function is used to save the state of a random function so that it can generate some random numbers on multiple executions of the code on the same machine or on different machines (for a specific seed value). The seed value is the previous value number generated by the generator. For the first time when there is no previous value, it uses the current system time.

Python3

import random

print("A random number between 0 and 1 is : ", end="")

print(random.random())

random.seed(5)

print("The mapped random number with 5 is : ", end="")

print(random.random())

random.seed(7)

print("The mapped random number with 7 is : ", end="")

print(random.random())

random.seed(5)

print("The mapped random number with 5 is : ", end="")

print(random.random())

random.seed(7)

print("The mapped random number with 7 is : ", end="")

print(random.random())

Output: 

A random number between 0 and 1 is : 0.510721762520941
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237

Method 4: Generating random number list in Python using shuffle()

It is used to shuffle a sequence (list). Shuffling means changing the position of the elements of the sequence. Here, the shuffling operation is in place.

Python3

import random

sample_list = ['A', 'B', 'C', 'D', 'E']

print("Original list : ")

print(sample_list)

random.shuffle(sample_list)

print("\nAfter the first shuffle : ")

print(sample_list)

random.shuffle(sample_list)

print("\nAfter the second shuffle : ")

print(sample_list)

Output:

Original list : 
['A', 'B', 'C', 'D', 'E']

After the first shuffle : 
['A', 'B', 'E', 'C', 'D']

After the second shuffle : 
['C', 'E', 'B', 'D', 'A']

Method 5: Generating random number list in Python using uniform()

This function is used to generate a floating point random number between the numbers mentioned in its arguments. It takes two arguments, lower limit(included in generation) and upper limit(not included in generation).

Python3

import random

li = [1, 4, 5, 10, 2]

print("The list before shuffling is : ", end="")

for i in range(0, len(li)):

    print(li[i], end=" ")

print("\r")

random.shuffle(li)

print("The list after shuffling is : ", end="")

for i in range(0, len(li)):

    print(li[i], end=" ")

print("\r")

print("The random floating point number between 5 and 10 is : ", end="")

print(random.uniform(5, 10))

Output: 

The list before shuffling is : 1 4 5 10 2 
The list after shuffling is : 2 1 4 5 10 
The random floating point number between 5 and 10 is : 5.183697823553464

How do I create a randomizer in Python?

import random n = random. random() print(n).
import random n = random. randint(0,22) print(n).
import random randomlist = [] for i in range(0,5): n = random. randint(1,30) randomlist. ... .
import random #Generate 5 random numbers between 10 and 30 randomlist = random. sample(range(10, 30), 5) print(randomlist).

Is there a randomizer in Python?

Python has a built-in module that you can use to make random numbers. ... Python Random Module..

How do you generate a random number between 1 and 10 in Python?

Use a random. randint() function to get a random integer number from the inclusive range. For example, random. randint(0, 10) will return a random number from [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 10].

How do you code a randomizer number?

Examples.
A Random number between 0 and 100. value = rand() * 100;.
A Random true or false. decision = (rand() > .5);.
A Random number between 50 and 100. x = (rand() * 50) + 50;.
A Random integer between 1 and 10. To get an integer from a floating point value we can use functions such as round or ceil or floor..