Randomly and uniquely iterating over a range

iteratorloopsrandom

Say you have a range of values (or anything else) and you want to iterate over the range and stop at some indeterminate point.

Because the stopping value could be anywhere in the range, iterating sequentially is no good because it causes the early values to be accessed more often than later values (which is bad for things that wear out), and also because it reduces performance since it must traverse extra values.

Randomly iterating is better because it will (on average) increase the hit-rate so that fewer values have to be accessed before finding the right one, and also distribute the accesses more evenly (again, on average).

The problem is that the standard method of randomly jumping around will result in values being accessed multiple times, and has no automatic way of determining when each value has been checked and thus the whole range has been exhausted.

One simplified and contrived solution could be to make a list of each value, pick one at random, then remove it. Each time through the loop, you pick one fromt he set of remaining items. Unfortunately this only works for small lists. As a (forced) example, say you are creating a game where the program tries to guess what number you picked and shows how many guess it took. The range is between 0-255 and instead of asking Is it 0? Is it 1? Is it 2?…, you have it guess randomly. You could create a list of 255 numbers, pick randomly and remove it. But what if the range was between 0-232? You can’t really create a 4-billion item list.

I’ve seen a couple of implementations RNGs that are supposed to provide a uniform distribution, but none that area also supposed to be unique, i.e., no repeated values.

So is there a practical way to randomly, and uniquely iterate over a range?

Best Answer

linear congruential generator (the x'=(a*x+c) mod m one) with some random seed

if you have m equal to the size of the array you will not get a repeat value until you cycle through all of them

the quality of their randomness isn't the greatest but if you just want to load balance it's fine

edit: one thing to keep in mind is that repeats are not likely on uniform distribution with a good RNG and a large range to generate from, so only excluding the last few is an option

so you can keep the last N numbers you took and exclude them from the available pool (just generate another number when it's a repeat),