![]() |
VOOZH | about |
Python Random module generates random numbers in Python. It introduce randomness into programs. It offers functions that support randomization operations, making it easier to work with unpredictable or varied outputs in different programming scenarios.
Selects a single random item from a list using random.choice().
3
Note: Every time you run this program, you may get a different output because the number is picked randomly.
Random numbers depend on the seeding value. For example, if the seeding value is 5 then the output of the below program will always be the same. Therefore, it must not be used for encryption.
The code sets the random number generator's seed to 5 using random.seed(5). It then prints two random floating-point numbers between 0 and 1 using random.random(). The seed makes these numbers the same every time we run the code with a seed of 5.
0.6229016948897019 0.7417869892607294
random.randint() method is used to generate random integers between the given range.
Syntax:
randint(start, end)
8 -7
Explanation:
A random.random() method is used to generate random floats between 0.0 to 1.
Syntax:
random.random()
0.8443722499369146
Explanation: random() generates a float number in the range [0.0, 1.0).
Shows how to use choice() with different sequence types.
2 g 2
Explanation: choice() picks a random element from a list, string, or tuple.
this example demonstrates the use of sample() to pick multiple items from sequences without repeating.
Syntax:
random.sample(sequence, length)
[4, 2, 3] [4, 7, 8] ['6', '4', '8']
Explanation: sample() returns a list of unique random elements from the input sequence.
random.shuffle() method is used to shuffle a sequence (list). Shuffling means changing the position of the elements of the sequence. Here, the shuffling operation is inplace.
After shuffle : [1, 4, 2, 5, 3] Second shuffle : [1, 4, 3, 5, 2]
Explanation: shuffle() changes the order of elements in the list randomly, in-place.
There are different random functions in the Random Module of Python. Look at the table below to learn more about these functions:
Function Name | Description |
|---|---|
| seed() | Initialize the random number generator |
| getstate() | Returns an object with the current internal state of the random number generator |
| setstate() | Used to restore the state of the random number generator back to the specified state |
| getrandbits() | Return an integer with a specified number of bits |
| randrange() | Returns a random number within the range |
| randint() | Returns a random integer within the range |
| choice() | Returns a random item from a list, tuple, or string |
| choices() | Returns multiple random elements from the list with replacement |
| sample() | Returns a particular length list of items chosen from the sequence |
| random() | Generate random floating numbers |
| uniform() | Return a random floating number between two numbers both inclusive |
| triangular() | Return a random floating point number within a range with a bias towards one extreme |
| betavariate() | Return a random floating point number with beta distribution |
| expovariate() | Return a random floating point number with exponential distribution |
| gammavariate() | Return a random floating point number with a gamma distribution |
| gauss() | Return a random floating point number with Gaussian distribution |
| lognormvariate() | Return a random floating point number with a log-normal distribution |
| normalvariate() | Return a random floating point number with normal distribution |
| vonmisesvariate() | Return a random floating point number with von Mises distribution or circular normal distribution |
| paretovariate() | Return a random floating point number with a Pareto distribution |
| weibullvariate() | Return a random floating point number with Weibull distribution |