Mercurial > emacs
comparison src/fns.c @ 10485:40c59e55775a
(Frandom): Call seed_random and get_random.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Thu, 19 Jan 1995 23:36:43 +0000 |
parents | b3c03881e6f6 |
children | 2a8f29cd9e9f |
comparison
equal
deleted
inserted
replaced
10484:08e1b7f85e5e | 10485:40c59e55775a |
---|---|
45 Lisp_Object arg; | 45 Lisp_Object arg; |
46 { | 46 { |
47 return arg; | 47 return arg; |
48 } | 48 } |
49 | 49 |
50 extern long get_random (); | |
51 extern void seed_random (); | |
52 extern long time (); | |
53 | |
50 DEFUN ("random", Frandom, Srandom, 0, 1, 0, | 54 DEFUN ("random", Frandom, Srandom, 0, 1, 0, |
51 "Return a pseudo-random number.\n\ | 55 "Return a pseudo-random number.\n\ |
52 All integers representable in Lisp are equally likely.\n\ | 56 All integers representable in Lisp are equally likely.\n\ |
53 On most systems, this is 28 bits' worth.\n\ | 57 On most systems, this is 28 bits' worth.\n\ |
54 With argument N, return random number in interval [0,N).\n\ | 58 With positive integer argument N, return random number in interval [0,N).\n\ |
55 With argument t, set the random number seed from the current time and pid.") | 59 With argument t, set the random number seed from the current time and pid.") |
56 (limit) | 60 (limit) |
57 Lisp_Object limit; | 61 Lisp_Object limit; |
58 { | 62 { |
59 int val; | 63 int val; |
60 unsigned long denominator; | 64 unsigned long denominator; |
61 extern long random (); | |
62 extern srandom (); | |
63 extern long time (); | |
64 | 65 |
65 if (EQ (limit, Qt)) | 66 if (EQ (limit, Qt)) |
66 srandom (getpid () + time (0)); | 67 seed_random (getpid () + time (0)); |
67 if (NATNUMP (limit) && XFASTINT (limit) != 0) | 68 if (NATNUMP (limit) && XFASTINT (limit) != 0) |
68 { | 69 { |
69 /* Try to take our random number from the higher bits of VAL, | 70 /* Try to take our random number from the higher bits of VAL, |
70 not the lower, since (says Gentzel) the low bits of `random' | 71 not the lower, since (says Gentzel) the low bits of `random' |
71 are less random than the higher ones. We do this by using the | 72 are less random than the higher ones. We do this by using the |
73 it's possible to get a quotient larger than limit; discarding | 74 it's possible to get a quotient larger than limit; discarding |
74 these values eliminates the bias that would otherwise appear | 75 these values eliminates the bias that would otherwise appear |
75 when using a large limit. */ | 76 when using a large limit. */ |
76 denominator = ((unsigned long)1 << VALBITS) / XFASTINT (limit); | 77 denominator = ((unsigned long)1 << VALBITS) / XFASTINT (limit); |
77 do | 78 do |
78 val = (random () & (((unsigned long)1 << VALBITS) - 1)) / denominator; | 79 val = get_random () / denominator; |
79 while (val >= XFASTINT (limit)); | 80 while (val >= XFASTINT (limit)); |
80 } | 81 } |
81 else | 82 else |
82 val = random (); | 83 val = get_random (); |
83 return make_number (val); | 84 return make_number (val); |
84 } | 85 } |
85 | 86 |
86 /* Random data-structure functions */ | 87 /* Random data-structure functions */ |
87 | 88 |