How to randomly select element from range in Python?


In this article, we will show how to randomly select from a range in python. Below are the various methods to accomplish this task −

  • Using random.randrange() function

  • Using random.randint() function

  • Using random.random() function

  • Using random.sample()

Using random.randrange() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the random module.

  • Use the random.randrange() function(Returns a random number within the specified range) to generate a random number within the given range by passing minimum, and maximum numbers as arguments.

  • Generate a random number from 1 to 50 alternatively(step-2) by passing start, stop and step values as arguments to the randrange() function.

Example

The following program returns a random numbers within the given range using random.randrange() function −

# importing random module import random print('Random Numbers from 1 to 50 are:') # getting a random number from 1 to 50 x=random.randrange(1,50) print(x) # getting an alternative(odd number) random number from 1 to 50 y=random.randrange(1,50,2) print(y)

Output

On executing, the above program will generate the following output −

Random Numbers from 1 to 50 are:
28
9

Using random.randint()

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Give the lower limit and higher limit values of the range and store them in two separate variables.

  • Use the random.randint() function(Returns a random number within the specified range) to generate a random number within the given range by passing minimum, and maximum numbers as arguments.

  • Print the generated random number.

Example

The following program returns random numbers within the given range using random.randint() function −

# importing random module import random # Giving a lower limit value of the range lowerLimit = 1 # Giving a higher limit value of the range higherLimit = 50 print('Random Number from',lowerLimit,'and',higherLimit,'is:') # getting a random number from 1 to 50 x=random.randint(1,50) # printing the generated random number print(x)

Output

On executing, the above program will generate the following output −

Random Number from 1 and 50 is:
18

Using random.random() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the random module.

  • Create a function getRandom(), that returns a random number by accepting the range of minimum and maximum numbers

  • Get the difference between maximum and minimum.

  • Generate a random number using the random() function(Returns a float value at random between 0 and 1)

  • Multiply a random number with a difference and converting to an integer using the int() function(converts the specified value into an integer).

  • Add the generated random number with a minimum value.

  • Return the generated random number.

  • Give the lower limit and higher limit values of the range and store them in two separate variables.

  • Call the getRandom() function by passing the above lower limit and upper limit values as an argument to it.

Example

The following program returns random numbers within the given range using random.random() function −

# importing random module import random # creating a function that returns random numbers by # accepts the range of minimum and maximum numbers def getRandom(minimum, maximum): # getting the difference between maximum and minimum diff = maximum - minimum # generating a random number using the random function randomNumber = random.random() # Multiplying a random number with a difference and converting it to an integer randomNumber = int(randomNumber * diff) # adding a random number with a minimum value randomNumber = randomNumber + minimum # returning the random number return randomNumber # Giving a lower limit value of the range lowerLimit = 1 # Giving a higher limit value of the range higherLimit = 50 print('Random Number from',lowerLimit,'and',higherLimit,'is:') # Passing lower limit and higher limit values as arguments to getRandom() function print(getRandom(10,100))

Output

On executing, the above program will generate the following output −

Random Number from 1 and 50 is:
12

Using random.sample()

The random.sample() method returns a list containing a randomly selected number of elements from a sequence.

Syntax

random.sample(sequence, k)

Parameters

  • sequence − any sequence like a list, tuple, etc.

  • k − number of random items to be generated

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the random module.

  • Enter the required number of random items to be generated.

  • Generate the required number of random numbers using random.sample() by passing the range of numbers(lower limit and upper limit), a number of random items to be generated as arguments to it.

The random.sample() method returns a list containing a randomly selected number of elements from a sequence.

Syntax

random.sample(sequence, k)
  • Print the generated list of random numbers.

Example

The following program returns random numbers within the given range using random.random() function −

# importing random module import random # Giving a lower limit value of the range lowerLimit = 1 # Giving a higher limit value of the range higherLimit = 50 print('Random Number from',lowerLimit,'and',higherLimit,'is:') # Entering the number of random items to be generated k=5 # generarting 5 random numbers between the lower limit and higher limit randomList = random.sample(range(lowerLimit, higherLimit),k) # printing the list of 5 random numbers print(randomList)

Output

On executing, the above program will generate the following output −

Random Number from 1 and 50 is:
[24, 32, 6, 29, 28]

Conclusion

In this article, we learnt four different Python methods for selecting a random item from a range. We also learned how to use the sample() function to create k random numbers within a particular range.

Updated on: 09-Nov-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements