Python Random module
The Python Random module is a built-in module for generating random integers in Python. These are sort of fake random numbers which do not possess true randomness. We can therefore use this module to generate random numbers, display a random item for a list or string, and so on.
Generate Random Floats
The random.random() function gives a float number that ranges from 0.0 to 1.0. There are no parameters required for this function.
random.random():- Returns The second random floating point value within [0.0 and 1) is returned.
random.uniform(a, b):- Generates a random floating point R in which a <= R <= b if a <= b and b <= R <= a if b < a.
random.expovariate(lambda):- Returns the random value according to exponential distribution.
random.gauss(mu, sigma):- Returns the random value according to gaussian distribution.
There are other distributions also, such as Gamma Distribution, Normal Distribution, etc.
Generate Random Integers
The random.randint() function generates a random integer from the range of numbers supplied.
Generate Random Numbers within a Defined Range
The random.randrange() function selects an item randomly from the given range defined by the start, the stop, and the step parameters. By default, the start is set to 0. Likewise, the step is set to 1 by default.
4 9 20
Select Random Elements
The random.choice() function selects an item from a non-empty series at random. An IndexError is thrown when the parameter is an empty series.
M 765 54
Shuffle Elements Randomly
A general sequence, like integers or floating-point series, can be a group of things like a List / Set. The random module contains methods that we can use to add randomization to the series.
The random.shuffle() function shuffles the entries in a list at random.
[23, 43, 86, 65, 34, 23] [65, 23, 86, 23, 34, 43]
We normally use the time of the system to ensure that the software delivers a different output each time we execute it because pseudorandom synthesis is dependent on the preceding number. As a result, we employ seeds.
We can specify a seed to have an initial number using Python's random.seed() function. This seed number determines a random number generator's outcome; therefore, if it stays the same, the outcome will continue to be the same.
Generating 5 random numbers: [29, 47, 44, 185, 87, 158] [29, 47, 44, 185, 87, 158]
Various Functions of Random Module
Following is the list of functions available in the random module.
We learned about various methods that Python's random module provides us with for dealing with Integers, floating-point numbers, and other sequences like Lists, tuples, etc. We also looked at how the seed affects the pseudo - random number pattern.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week