Algorithm for generating a random number

No your algorithm is not scalable. What I’ve done before is to issue numbers serially (+1 each time) and then pass them through an XOR operation to jumble the bits thus giving me a seemingly random numbers. Of course they aren’t really random, but they look so to users eyes.


[Edit] Additional information

This algorithm’s logic goes like this you use a known sequence to
generate unique numbers and then you deterministically manipulate them,
so they don’t look serial anymore. The general solution is to use
some form of encryption, which in my case was an XOR flipflop, because
its as fast as it can get, and it fulfills the guarantee that numbers
will never collide.

However you can use other forms of encryption, if you want prefer even more
random looking numbers, over speed (say you don’t need to generate many
ids at a time). Now the important point in choosing an encryption algorithm
is “the guarantee that numbers will never collide”. And a way to prove if an encryption algorithm can fulfill
this guarantee is to check if both the original number and the result of
the encryption have the same number of bits, and that the the algorithm is
reversible (bijection).

[Thanks to Adam Liss & CesarB for exapanding on the solution]

Leave a Comment