Social Widget

Responsive Ads Here

Friday, December 13, 2019

Generate random numbers using python

Random module in python

The random module provides access to functions that support many operations. Perhaps the most important thing is that it allows you to generate random numbers. These functions are used in a lot of games, lotteries or any application requiring random number generation.

Random functions

The Random module contains some very useful functions. They are

1. Shuffle

This function is used to shuffle the entire list to randomly arrange them.

2.  Random

This function generates a float random number less than 1 and greater or equal to 0.

3. Choice

The choices() method returns a list with the randomly selected element from the specified sequence.

4. Randrange

This function is also used to generate random numbers but within a range specified in its arguments. This function takes 3 arguments, beginning number (included in generation), last number (excluded in generation) and step ( to skip numbers in range while selecting).

5. Randint

A random integer within the given range as parameters. "randint()" accepts two parameters: the lowest number and the highest number. The first parameter should be less than the second number. example (1,5), (88, 200) etc.


1. When we want the computer to pick a random number in a given range.
2. Pick a random element from a list, pick a random card from a deck, flip a coin, etc.
3. When making your password database more secure or powering a random page feature of your website.


The following python snippet generates a random number of specified digits. "n" takes an integer and generates a random number of n digits.

#python #random #randint #randomfunction

No comments:

Post a Comment