Loading...

Python Random module – How to generate random numbers/data

Python Random module – How to generate random numbers/data

Lottery tickets, gambling, Recaptcha, gaming: what is one thing common among all? Randomness. Lottery tickets have random numbers; gambling games like roulette at casinos produce random numbers; the captcha you fill out to prove yourself as a human has random images; games like PUBG, CS: GO, and Apex Legends give random rewards in chests; etc. Generating random numbers and data can be easily and efficiently done using a computer rather than a human. Python provides a random module for this that can be used to generate this randomness. Without further ado, let’s get right into the article.

Introduction

To produce a wide range of random numbers and values, the Python random module is a built-in function library. It enables the generation of random values, the selection of random sequence components, and the generation of random permutations of a given sequence. Additional uses for random numbers include simulation, cryptography, security, machine learning, and data analysis. The random(), randint(), randrange(), choice(), and random.seed() are examples of a few of the functions and constants included in the random module of the Python programming language. Then, let’s discuss these random module functions along with their examples.

The random() Function

The random() method provides a randomly generated floating-point number N with accuracy up to 16 decimal places and requires no parameters. This function is used to create a random floating-point number between 0 and 1 (including both). It requires no arguments. For example:

'''Program to display a random number using random() function''' #Before using any module we have to import it first import random rand_no = random.random() print("Random number: ",rand_no)
Code language: Python (python)
Output

The randint() Function

The randint() function accepts the two inputs a and b and produces a random number N such that a <= N <= b. This function is used to produce a random number within a specified range (a,b). The range of possible values must be an integer. For instance:

'''Program to display a random integer using randint() function''' #We can give a name to the module while importing it import random as r r_int = r.randint(25,100) #giving two parameters a and b is a must to avoid syntax error print("Random integer: ",r_int)
Code language: Python (python)
Output

The randrange() Function

Using start, stop, and step as its first two or three inputs, the randrange() method generates a random integer N such that start <= N <stop and N is a multiple of step. This method is utilized to produce random numbers inside a specified range (start, stop), with a specified step (step). The set of possible values only contains integers. Consider the following code for the same:

'''Program to display a random number from a range of numbers using randrange() function''' import random as r r_range_no = r.randrange(-100,101) #we can also give negative values in the parameter print("Random number: ",r_range_no)
Code language: Python (python)
Output

The choice() Function

A random element from the sequence is returned by the choice() function, which only accepts one input, a sequence. Using a specified sequence, this function can produce a random element. For example:

'''Program to display a random number from a sequence using choice() function''' import random as r List=[90,'rocket',64,1,'college','Banana'] Tuple=(32,57,69,58) #You can use choice() function for both Lists and Tuples #you can have any type of values in the sequences choice_L = r.choice(List) choice_T = r.choice(Tuple) print("Random element from list: ",choice_L) print("Random element from tuple: ",choice_T)
Code language: Python (python)
Output

The random.seed() Function

The random.seed() function accepts a single argument, a seed, and configures the seed to produce a predefined stream of random numbers. The starting point of a series of random numbers can be determined using this function. For example:

'''Program to set a seed for random() function''' import random as r r.seed(11) print(r.random()) print(r.randint(0,123)) r.seed(11) print(r.random()) print(r.randint(0,123))
Code language: Python (python)
Output

Note: The generator creates a random number based on the seed value, so if the seed value is 7, you will always get 0.32383276483316237 as the first random number every time you set the same seed. Also, if you will use any other random module function after setting the same seed you will get the same number from the function as well.

The shuffle() Function

The elements of a list can be randomized and displayed using random.shuffle() function. It rearranges the list’s components in a new order. It is an in-place function, which implies that it changes the list, tuple, or string directly rather than returning a modified version of the original list. For example,

'''Program to Shuffle the items present in a list using shuffle() function''' import random as r list_x=[121,'Ram',33,92,'Sita','Hanuman'] print("Original List: ") print(list_x,"\n") r.shuffle(list_x) print("Shuffled List: ") print(list_x,"\n") r.shuffle(list_x) print("Shuffled List again:") print(list_x)
Code language: Python (python)
Output

The uniform() Function

To get a random float number between two provided integers, use the random.uniform() method. This function gives a random floating point number N such that A <= N <= B if two values are provided, let’s say A and B (where A is the lower limit and B is the higher limit). For example:

'''Program to display random float number between two parameter using uniform() function''' import random as r uni_no=r.uniform(25.32,89.99) print("Random Number: ",uni_no)
Code language: Python (python)
Output

The sample() function

Python’s sample() method returns a list of randomly selected elements from the supplied list or sequence. It may be used to choose one item at random from a list for testing or experimenting. For example:

'''Program to display random sample of list of a specified number of items using sample() function''' import random as r season_list = ["Winter", "Summer", "Spring","Autumn","Monsoon"] print(r.sample(season_list,3))
Code language: PHP (php)
Output

Benefits of using the random() module

There are two key advantages of using the random module function in Python. First, improved effectiveness The amount of code that has to be written can be significantly decreased by developers by utilizing the features of the Random Module. Faster development times and better performance may result from this. Additionally, enhanced security A software can be shielded from malicious assaults or nefarious efforts to obtain sensitive data by writing random data into it.

Applications of Python Random Module

The Python Random Module Function has two primary goals. The first stage is to generate random numbers, which may subsequently be applied to various applications such as simulation and gaming software, scientific computations, and more. The second option is to come up with random options, such as picking a random item at random from a list. This is quite beneficial for applications that need random elements.
With the help of the Random Module Function, you may generate random lists of characters, numbers, and other types of data. Additionally, shuffle operations—which entail randomly altering the order of list members—can be carried out using it. These operations are quite useful in more complicated applications, such as sorting algorithms.

Conclusion

It’s time we conclude our article today. We discussed Python’s Random Module and the functions present in the module. These included the random(), randint(), randrange(), choice(), random.seed(), shuffle(), uniform(), and sample() functions, along with the benefits and applications of the Random module. I hope you learned something new today that will make you one step closer to your goal. If you have any queries, feel free to comment down below. Till then, keep coding, and have a wonderful day ahead!

Sharing is caring

Did you like what Pravin Gupta wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far