Javatpoint Logo
Javatpoint Logo

Python Program to Generate a Random Number

Generating a random number in the programming world becomes crucial when applications require a random number generator. Random number generation is generally used in lotteries, games, and many applications.

Python provides several ways to generate random numbers. In Python, we can generate a random integer, doubles, long, etc., in various ranges by importing the "random" module.

In this article, we will explore various methods to generate a random number in Python.

Using the random Module to Generate a random number

In the following examples, we will learn how to generate random numbers using the random module.

Syntax:

First, we have to import the random module and then apply the syntax:

1. Generating a Random Number - Using the random() function

The random module provides a random() method that generates a float number between 0 and 1. Let's see the following example.

Output:

Random Number between 0 to 1: 0.11835380687392505
Random Number between 0 to 1: 0.2563622979831378
Random Number between 0 to 1: 0.4344921246881883

Explanation:

In the above example, we have used the random function to generate a random number between 0 to 1, both inclusive. When we execute the random function again, we will get different outputs as above.

2. Generating a Number within a Given Range - Using the randint() function

Python random module provides the randint() method that generates an integer number within a specific range. We can pass the two numbers as arguments that define the range. Let's understand the following example.

Example - 1: Generating a random integer between 0 to 50 using the randint() function

Output:

Random Number between 0 to 50: 12
Random Number between 0 to 50: 18
Random Number between 0 to 50: 8

Explanation:

In the above example, the specified range is 0 to 50. The randint functions cleverly generate a random number between 0 and 50, both inclusive. When we execute the function again, we get a different random number within the same range.

Example - 2: Generating a random integer between 100 to 200 using the randint() function

Output:

Random Integer between 100 to 200: 175
Random Integer between 100 to 200: 145
Random Integer between 100 to 200: 167

Explanation:

Here also, the randint() function generates a random number between the specified range which is 100 to 200. Every time we call the randint() function, it returns a number within the same range.

Using a for-loop to create a list of random integers

The randint() method can be used with for loop to generate a list of random numbers. To do so, we need to create an empty list and then append the random numbers generated to the empty list one by one. Let's understand the following example.

Example - Creating a list of random integers using for loop

Output:

List of random integers: [50, 25, 43, 48, 24, 20, 39, 45, 32, 28]

Explanation:

In the above example, we first imported the random module. Following that, we created a list to store random numbers. By using a for-loop, we generated ten random integers iteratively and stored them in the rand_list. Finally, we printed the list into the console.

We can easily create a list of random integers by using the sample function, see the example below:

3. Generating a list of random integers using the random.sample()

The random module also provides the sample() method, which directly generates a list of random numbers. Below is an example of generating random numbers using the sample() method.

Example - Generating a random number list using sample() function

Output:

List of random integers one [34, 21, 15, 25, 17, 10, 37, 18, 20, 30]
List of random integers two [4, 8, 5, 9, 1]

Explanation:

In the above code, we have used the range() function, which generates the numbers between the given range. The sample function then returns a list of length = 10 containing random integers chosen from the range(10, 40). Also, we have used a list of integers and passed it to the sample function. The sample function chooses five random numbers from the list and returns a list containing those random numbers. We can also pass a string, tuple, and set to the sample function.

4. Generating a Random Number Using the Uniform() in Python

The uniform() function is used to generate random floating-point numbers in Python. The uniform() function allows us to generate random numbers within a specified range.

Let us see how to use the uniform() function to generate a random number in Python.

Output:

Random Number 1: 0.9036830628327847
Random Number 2: 0.5129018404378839
Random Number 3: 0.10637532891792578

Explanation:

In the above example, we have used the uniform function that generates a random floating-point number between the specified range. The uniform function takes two arguments, lower bound and upper bound, and returns a random number between them. Here, the range is 0 to 1, both inclusively. And the random numbers generated by the uniform function are within this range.

You can also modify the range to generate a random number within a different range. See the sample code below:

Here, the uniform function generates random numbers between the specified range.

It is worth noting that the uniform function uses a pseudo-random number generator. We can set a seed value using the random.seed() function. It will intialize the random number generator.

This means that if we use the same seed value, we will get the same sequence of random numbers every time we run your program.

Here is an example of how to use random.seed():

Output:

6.754841186120954
1.2250967970040025
3.4752638653220735

When we run the same program, we get the same output:

6.754841186120954
1.2250967970040025
3.4752638653220735

Before moving to the end of the discussion, let us understand the concept of shuffling.

Randomly Reordering a List by using random.shuffle() function:

The idea of shuffling refers to the randomly reordering the elements of a sequence. In Python, the random module includes shffule() function that randomly reorders the elements of a list.

Example to demonstrate how to use the shuffle() function to shuffle a list in Python:

Output:

Before Shuffling: [1, 2, 3, 4, 5]
After first Shuffling: [2, 3, 1, 5, 4]
After second Shuffling: [4, 1, 5, 2, 3]

Explanation:

In the above code, we have used the shuffle function to reorder the elements of my_list. The shuffle() function randomly reorders the elements within my_list. Finally, we print the shuffled list.

Also, we apply shuffling to a string by first converting it into a list, shuffling the list, and joining the elements back into a string. Here is how you can do this:

Output:

Original string: Hello, World!
Shuffled String: H,oelWl!dro l

Explanation:

In the above example, we first converted the string into a list, shuffled the list by using random.shuffle() function, and then converted the list back into a string. Finally, we printed the shuffled string.

CONCLUSION

Python provides several methods to generate a random number within the random module. We explored different methods to generate a random number using the random(), randint(), sample(), and uniform() functions.

Also, we discussed that random.seed() is used to set the seed value and random.shuffle() function can be used to shuffle the elements of a list randomly.

We can use these flexible methods from the random module in Python to generate a random number according to our specific requirements.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA