Skip to content

using random.choice to sample integers in a large range #5299

@Midnighter

Description

@Midnighter

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions