jeudi 14 octobre 2021

Iterate over a random permutation of numbers in the range 0..n with large n

I need to iterate over a random permutation of integers in the range 0..n.

My first idea was to create an array of sequential integers and shuffle it. It works well for small n but gets slow and uses too much memory with larger ns (n > 1000000).

How can I create an iterator which returns non-repeating integers in the range 0..n without allocating an array of size n?




Aucun commentaire:

Enregistrer un commentaire