Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
emacs
emacs
Commits
78217ef1
Commit
78217ef1
authored
Mar 16, 1994
by
Karl Heuer
Browse files
(Frandom): Eliminate bias in random number generator.
parent
81a63ccc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
8 deletions
+13
-8
src/fns.c
src/fns.c
+13
-8
No files found.
src/fns.c
View file @
78217ef1
...
...
@@ -55,24 +55,29 @@ With argument t, set the random number seed from the current time and pid.")
Lisp_Object
limit
;
{
int
val
;
unsigned
long
denominator
;
extern
long
random
();
extern
srandom
();
extern
long
time
();
if
(
EQ
(
limit
,
Qt
))
srandom
(
getpid
()
+
time
(
0
));
val
=
random
();
if
(
XTYPE
(
limit
)
==
Lisp_Int
&&
XINT
(
limit
)
!=
0
)
if
(
XTYPE
(
limit
)
==
Lisp_Int
&&
XINT
(
limit
)
>
0
)
{
/* Try to take our random number from the higher bits of VAL,
not the lower, since (says Gentzel) the low bits of `random'
are less random than the higher ones. */
val
&=
0xfffffff
;
/* Ensure positive. */
val
>>=
5
;
if
(
XINT
(
limit
)
<
10000
)
val
>>=
6
;
val
%=
XINT
(
limit
);
are less random than the higher ones. We do this by using the
quotient rather than the remainder. At the high end of the RNG
it's possible to get a quotient larger than limit; discarding
these values eliminates the bias that would otherwise appear
when using a large limit. */
denominator
=
(
unsigned
long
)
0x80000000
/
XFASTINT
(
limit
);
do
val
=
(
random
()
&
0x7fffffff
)
/
denominator
;
while
(
val
>=
limit
);
}
else
val
=
random
();
return
make_number
(
val
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment