Generate pseudo-random 16 bit number
I want to generate a 16 bit pseudo-random number using only 16 bit variables. This will then be used to generate pseudo-random numbers in the range mn to mx (typically 10 to 700). I've come across this code in c:
Code:
short x = 3030;
unsigned xorshift( )
{
x ^= x << 7;
x ^= x >> 9;
x ^= x << 8;
return x;
}
short getrnd(short mn, short mx) {
short r = xorshift();
short mod = mx - mn + 1;
r = r % mod;
r = r + mn;
return r;
}
The language is un-important. I'm more interested in the algorithm used. The algorithm needs to be fairly simple and quick. This seems to be ok but is there a better algorithm than this please?
Re: Generate pseudo-random 16 bit number
https://github.com/ZiCog/xoroshiro
It looks like xoroshiro32++ is a better 16-bit PRNG as it uses rotation in addition to xor and shift only.