2

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?

  • Related randomly and uniquely iterating over a range and the key consideration to that approach is that you don't need a 4B item list, you only need 4B bits (for 2^32) which is 512MB. It really depends on what other data requirements and how random the numbers are. – user40980 Nov 7 '13 at 22:20
  • @MichaelT, 4B bits is a list. And that’s the problem because a single bit is the simplest kind of list possible, and yet 512MB is excessive for what should be a fairly simple idea. – Synetech Nov 7 '13 at 23:23
  • The question that you need to look at though is "do you want pseudo-random points from that list or do you want them in a non-random (high serial coefficient) order?" If you want random, you need the list because you cannot predict where the next point will be. If you want "efficient coverage" the function mentioned in the answers works superbly, but isn't the slightest bit random. – user40980 Nov 8 '13 at 3:37
7

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),

  • Well, learn something new every day... I'm going to have to poke my other answer with that as another approach. – user40980 Nov 7 '13 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.