Reputation: 4231
I want a pseudorandom source of bits that I can look through with an index; my particular use case is random replay of a playlist, where I want to be able to rewind to earlier songs without storing the order in which the songs were played in the first place. Most RNGs work with a state that is modified with each new random number that is generated, and the previous state is not easily retrieved.
Now I had this idea: use some sort if seed, and compute a hash code from it. after the bits from the hash code are used up, increase the seed, and compute the next hash. as the seed is only modified reversibly, older hash codes and therefore "random" bits can be retrieved.
Now my actual question: How random is this from a theoretical point of view? it's not really important for a music playlist, but I'm still interested in it. I can also imagine computer game applications where fairness would be a concern.
Clearly, there is not much entropy involved, but a (cryptographic) hash function is supposed to have entirely different output on the change of a single bit. Can I improve randomness by doing some other reversible operation on the seed than increasing by one?
Upvotes: 0
Views: 1265
Reputation: 11892
Nice idea. Maybe a bit of an overkill, if you want to generate a lot of random numbers.
If the result is really "random" or not depends on the encryption algorithm you use. Good onces (SHA, ...) will give a result that has an equal distribution. Equal distribution is one of the requirements for such algorithms.
Keep in mind, that encryption/hash algorithms are way more complex, then random number generators. Therefore generating massiv amount of such numbers will be an issue. This might effect games, probably no problem for generating a playlist.
BTW: Have you seen Collections.shuffle()
. It might ease your live.
Upvotes: 2