Fun with Xorshift

I’ve been hacking on an old game I wrote for the school paper I founded: Hangman. (Code: https://github.com/fadend/hangman.) One feature I like: the ability to create your own games with a shareable link. Here’s one for you: https://revfad.com/hangman/?w=JVFUVAT+LBH+N+JBAQRESHY+QNL.

Since I didn’t want the URL parameter to give away the answer, I have the game run the letters through rot13. This has the flaw though that the same letter always gets the same mapping, giving a little bit too much away. Probably just making the offset increment per index or even just alternating per index would have been enough, but I got curious about what pseudorandom number generator I might use instead to choose the offsets.

This led me to George Marsaglia‘s Xorshift and his 2003 paper introducing it: Xorshift RNGs. The paper lists 81 triples of constants a, b, c plus 8 variations on XOR + shift operations to get to the next output.

Here’s a little program to visualize the output: https://revfad.com/xorshiftfun/. (Code: https://github.com/fadend/xorshiftfun.)

One surprising thing: at least as I’ve tried to implement it in JavaScript, the following update steps seem to only yield outputs covering half of the possible space:

y^=y>>a; y^=y<<b; y^=y>>c;
y^=y>>c; y^=y<<b; y^=y>>a;
y^=y<<a; y^=y<<c; y^=y>>b;
y^=y<<c; y^=y<<a; y^=y>>b;

You can see this visually in the output, e.g.:

Not sure if I’ll get to investigating this more deeply since I’ve got some other higher priority things to look at, but I’m a little curious what’s going wrong.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *