mercredi 24 février 2016

Generate random numbers without using the last n values in Python

I have a Python function that generates a random number between 0 and 100:

def get_next_number():
    value = randint(0,100)

Every time I call this function, I need it to return a random number but that number cannot be one of the last n random numbers it returned (lets say 5 for this example).

Here are some examples:

55, 1, 67, 12, 88, 91, 100, 54 (This is fine as there are no duplicates in the last 5 numbers returned)

77, 42, 2, 3, 88, 2... (When the function gets a random number of 2, I need it to try again since 2 was already returned 3 numbers prior)

89, 23, 29, 81, 99, 100, 6, 8, 23... (This one is fine because 23 occurred more than 5 times before)

Is there something built into the random function to accomplish this?




Aucun commentaire:

Enregistrer un commentaire