question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using `random.choice` to sample integers in a large range

See original GitHub issue

So 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:closed
  • Created 9 years ago
  • Reactions:6
  • Comments:16 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
bashtagecommented, Jun 1, 2019

@mattip Fixed in Generator, can’t be fixed in RandomState

In [1]: import numpy as np
   ...: max_int = np.iinfo(np.uint32).max
In [2]: g = np.random.Generator()
In [3]: %timeit numbers = g.choice(max_int, 10, replace=False)
19.2 µs ± 1.72 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
0reactions
mattipcommented, Jun 1, 2019

Ok, closing. For performance use the new APIs.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found