VBForums - General Developer Forum
https://www.vbforums.com/
Discuss issues important to developers, ask for career advice, debate over which new technology is going to take over the software scene, ponder certifications, and contemplate over the meaning of all things technical.enFri, 15 Nov 2024 00:19:11 GMTvBulletin60https://www.vbforums.com/images/misc/rss.pngVBForums - General Developer Forum
https://www.vbforums.com/
Generate pseudo-random 16 bit number
https://www.vbforums.com/showthread.php?909009-Generate-pseudo-random-16-bit-number&goto=newpost
Thu, 14 Nov 2024 16:58:54 GMT> 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?]]>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?
]]>General Developer Forum2kaudhttps://www.vbforums.com/showthread.php?909009-Generate-pseudo-random-16-bit-number