1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-12-21 16:46:24 -05:00

Improve ernd as &rnd( 2) always returned same pattern.

This commit is contained in:
Renaud 2017-05-14 15:18:28 +08:00
parent ba87641122
commit 918a310601

14
eval.c
View File

@ -1321,18 +1321,18 @@ char *mklower(char *str)
/* /*
* returns a random integer * returns a random integer
* ernd( 0) [ 0 .. 2147483647] * ernd( 0) [ 0 .. 2147483647]
* ernd( i) [ 1 .. abs( i)] * ernd( -2147483648) [ 0 .. 2147483647]
* ernd( 1) [ 1] * ernd( 1) [ 1]
* ernd( -2147483648) [ 1 .. 2147483647] actually 2147482413 * ernd( i) [ 1 .. abs( i)]
*/ */
static int ernd( int i) { static int ernd( int i) {
seed = seed * 1721 + 10007 ; int s ;
seed &= ~(1 << 31) ; /* avoid abs() which introduces 176719 periodicity */
if( i == 0)
return seed ;
seed = seed * 1721 + 10007 ;
s = ((seed >> 16) & 0x0000FFFF) | (seed << 16) ;
s &= ~(1 << 31) ; /* avoid abs() */
i = i < 0 ? -i : i ; /* abs( i) */ i = i < 0 ? -i : i ; /* abs( i) */
return seed % i + 1 ; return (i <= 0) ? s : s % i + 1 ;
} }
/* /*