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:
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?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; }


Reply With Quote