using `random.choice` to sample integers in a large range
See original GitHub issueSo I wanted to generate some unique integers with the following code:
import numpy as np
max_int = np.iinfo(np.uint32).max
numbers = np.random.choice(max_int, 10, replace=False)
I wanted these numbers as deterministic seeds for some simulations. When I do this almost 16 GB of memory are filled.
I looked into the code for choice and in this case it essentially generates a permutation, similar to shuffling a np.arange(max_int)
, in order to then take a small slice from that array. This seems like a bad strategy since providing only an int
to random.choice
makes it clear that numbers in that range should be sampled. At least when the integer argument a
is much larger than the argument size
.
So for now I will just generate a set of numbers but I was wondering if there is a more general way.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:6
- Comments:16 (15 by maintainers)
Top Results From Across the Web
Python random randrange() and randint() to ... - PYnative
Use a random.randrange() function to get a random integer number from the given exclusive range by specifying the increment. For example, random ......
Read more >random — Generate pseudo-random numbers — Python 3.11 ...
To choose a sample from a range of integers, use a range() object as an argument. This is especially fast and space efficient...
Read more >Creating random sample larger that range - Stack Overflow
You could use random.choices : random.choices(range(60, 126), k=500). This will return a list of 500 values where each value comes from the ...
Read more >How to Use Numpy Random Choice - Sharp Sight
The NumPy random choice function is a lot like this. Given an input array of numbers, numpy.random.choice will choose one of those numbers...
Read more >Python | Generate random numbers within a given range and ...
In python, there's an inbuilt method, “random.uniform()” which performs this task with ease and uses just one word. This method is defined in ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@mattip Fixed in Generator, can’t be fixed in RandomState
Ok, closing. For performance use the new APIs.