- 论坛徽章:
- 2
|
sys/lib/libkern/arch/i386/random.S
/* Here is a very good random number generator. This implementation is
* based on ``Two Fast Implementations of the "Minimal Standard" Random
* Number Generator'', David G. Carta, Communications of the ACM, Jan 1990,
* Vol 33 No 1. Do NOT modify this code unless you have a very thorough
* understanding of the algorithm. It's trickier than you think. If
* you do change it, make sure that its 10,000'th invocation returns
* 1043618065.
*
* Here is easier-to-decipher pseudocode:
*
* p = (16807*seed)<30:0> # e.g., the low 31 bits of the product
* q = (16807*seed)<62:31> # e.g., the high 31 bits starting at bit 32
* if (p + q < 2^31)
* seed = p + q
* else
* seed = ((p + q) & (2^31 - 1)) + 1
* return (seed);
*
* The result is in (0,2^31), e.g., it's always positive.
*/
#include <machine/asm.h>
.data
randseed:
.long 1
.text
ENTRY(random)
movl $16807,%eax
imull randseed
shld $1,%eax,%edx
andl $0x7fffffff,%eax
addl %edx,%eax
js 1f
movl %eax,randseed
ret
1:
subl $0x7fffffff,%eax
movl %eax,randseed
ret
数学不够,看不懂......
>>>It's trickier than you think   |
|