To generate random numbers without repeating in Python, you can use the random module function choices(). choices() takes a list and the number of random numbers you want to generate.
import random
lst = range(0,100)
print(random.choices(lst, k=10))
#Output:
[37, 95, 88, 82, 15, 38, 60, 71, 56, 49]
When working with data, it can be very useful to generate random numbers to be able to perform simulations or get a random sample of a dataset.
In Python, we can generate random numbers in a range easily. The Python random module has many useful functions for generating random numbers.
The generation of random numbers is pretty easy, but sometimes we need to generate random numbers and have all of our numbers be unique.
In Python, you can easily generate random numbers without repeating.
To generate random numbers without repeating in Python, you can use the random module function choices(). choices() takes a list and the number of random numbers you want to generate.
Below is a simple example of how to generate random numbers between 0 and 100 without repeating in Python.
import random
lst = range(0,100)
print(random.choices(lst, k=10))
#Output:
[37, 95, 88, 82, 15, 38, 60, 71, 56, 49]
Generating Random Numbers from a List of Numbers Without Repeating in Python
If you have a list of numbers that you want to get random numbers from without repeating, there is an extra step you need to take.
First, if you have a list of numbers, we need to remove duplicates so that we can use choices().
To remove the duplicates from your list, you should convert it to a set with set(). Then, you can convert it back to a list and use choices().
Below is an example of how to generate non-repeating random numbers with Python from a list of numbers with duplicates.
import random
lst = [0,0,1,2,2,3,4,5,5,5,6,7,8,9,9,9,10]
lst_without_dups = list(set(lst))
print(random.choices(lst_without_dups, k=5))
#Output:
[9, 0, 8, 3, 6]
Hopefully this article has been useful for you to learn how to generate a list of random numbers without repeating in Python.
Interesting approach. I’m trying to produce a random list when running the same random code generator program asynchronously (simultaneously) where the output list of all combined programs are unique.
Any ideas how to do this? I’m thinking along the lines of using your approach but after each program uses a value, remove it from the list/ set. Think that’ll work?
Thanks for the comment.
Are you describing the following output?
or something like this?
If you are starting the different python programs from the same script as your random code generator, then you could pass the random code generator as an object and the first case can be solved.
If you the second one is what you desire, then you can send your random code generator a different seed to random.seed() for each program to guarantee that the list is unique (but the numbers may not be unique)
It’s most like the 2nd scenario. Thing is, it’s the same program being run from different Celery workers. I don’t think I can change the code for each Celery worker. Using the technique described in this article should work OK when only one worker runs the program, however what’s unique here is I’m doing Async processing using Celery and I need each instance to produce a unique list from the same code.
Thanks,
Is it possible to keep a variable representing a seed which will be updated each time the Celery worker performs its work? Then each time the Celery worker is done, the seed can be updated.
In theory, this could work. Otherwise what you could do is generate all possible permutations of your list and then just select one by one until the list is complete.