How To Generate Random Numbers In The Range Excluding Some Numbers In Python

This article will share with you how to generate random numbers in the range excluding some numbers in Python. It is widely used in practical applications such as in a typical game called playing cards. The cards distribution is based on generating random numbers in the range excluding some numbers.

Generate Random Numbers In Range Excluding Some Numbers In Python

To generate random numbers in the range excluding some numbers in Python, you can use the choice() method, the randint() method, and the choices() method in the ‘random’ module.

Randomize a number in the range excluding some numbers with the choice() function

To randomize a number in the range excluding some numbers with the choice() function, you should follow these steps.

  • Step 1: Create a list that does not contain the specified numbers.
  • Step 2: Randomize a number in this list by the choice() function.

Look at the example below.

import random

def generateRandomNumbers(st, en, exclusion=list()):
    # Create a list whose value is from 'st' to 'en'
    numList = list(range(st, en+1))

    # Create a list that does not contain the exclusive value
    newList = [x for x in numList if x not in exclusion]

    # Generate a random number excluding some numbers
    result = random.choice(newList)
    return result

# Try this function
exclusion = [1, 3, 5, 7, 9]
print(generateRandomNumbers(1, 10, exclusion))

Output

2

Randomize a number in the range excluding some numbers with the randint() function

In this solution, we will recur the function until we get the correct result. So, it will take longer than the first solution.

Look at the example below.

import random

def generateRandomNumbers(st, en, exclusion=list()):
    # Generate a random number excluding some numbers
    result = random.randint(st, en)
    return result if result not in exclusion else generateRandomNumbers(st, en, exclusion)

# Try this function
exclusion = [1, 3, 5, 7, 9]
print(generateRandomNumbers(1, 10, exclusion))

Output

2

Randomize numbers in the range excluding some numbers with the choices() function

In this solution, we will randomize the specified numbers in the range excluding some numbers with the choices() function. You should look back at the first solution to know how this solution operates.

Look at the example below.

import random

def generateRandomNumbers(st, en, amount, exclusion=list()):
    # Create a list whose value is from 'st' to 'en'
    numList = list(range(st, en+1))

    # Create a list that does not contain the exclusive value
    newList = [x for x in numList if x not in exclusion]

    # Generate k random numbers excluding some numbers
    result = random.choices(newList, k=amount)
    return result

# Try this function
exclusion = [1, 3, 5, 7, 9]
print(*generateRandomNumbers(1, 10, 5, exclusion))

Output

2 2 6 10 8

Besides, you can generate random letters in my previous tutorial here.

Summary

We have shown you how to generate random numbers in the range excluding some numbers in Python. To do that, you can use the choice() method, the randint() method, or the choices() method in the random package. But you should not use the randint() function with this case because it will take longer than others. We hope this tutorial is helpful to you. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *