Long time ago a friend told me about a very simple algorithm but with beautiful results. It’s a simple inversion of a number’s bits. Despite being nothing amazing “as is” it may have some cool results. In his math paper about he explain how good it is to scramble numbers to have a feeling of randomness but still assuring that every number on the range will show up exactly one time, no more, no less.
For instance, take a number with four bits: 15 [1111]. If you take every number only once between 1 and 15 you’ll never repeat the same number, right ? So, if I take every number from 1 to 15 but, before showing the result to the user, I invert it’s bits. The output won’t be a sequence but you still iterate through a sequence, thus, the output looks like random but in fact it’s just a weird iteration.
The sequence would be like:
Original Output 01: 0001 08: 1000 02: 0010 04: 0100 03: 0011 12: 1100 04: 0100 02: 0100 (...) 15: 1111 15: 1111
There are some draw backs, of course. One of them is that it’s not even pseudo-random, other is that all even number comes before all odd numbers (because of the first bit being the last), but also have two good uses I’ve seen:
– Reder images in a non-linear sequence, giving you an idea of how it’ll look like quite sooner
– Create IDs for websites on a non-trivial sequence making harder for people trying to guess the user’s IDs
But again, it won’t block people from seeing anything anywhere, it’s just a way of scrambling. But today, sites are using hashes to do the same effect, better but still not safe.
Another good thing is that if you change the offset (or how many bits to swap like 0001 or 00000001) the sequence will be completely different.
Anyway, if you’re curious check the code.