diff options
| author | Karl Heuer | 1995-01-19 23:36:43 +0000 |
|---|---|---|
| committer | Karl Heuer | 1995-01-19 23:36:43 +0000 |
| commit | 99175c23d34107a2d4d7b1eb00d40b4c0d6704a3 (patch) | |
| tree | f2dd0bcbb5439a6889445347bb89d248aecffc8f /src | |
| parent | 66263a7d6e5026f71e679353ee81e33f7a0e9244 (diff) | |
| download | emacs-99175c23d34107a2d4d7b1eb00d40b4c0d6704a3.tar.gz emacs-99175c23d34107a2d4d7b1eb00d40b4c0d6704a3.zip | |
(Frandom): Call seed_random and get_random.
Diffstat (limited to 'src')
| -rw-r--r-- | src/fns.c | 15 |
1 files changed, 8 insertions, 7 deletions
| @@ -47,23 +47,24 @@ DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0, | |||
| 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, |
| @@ -75,11 +76,11 @@ With argument t, set the random number seed from the current time and pid.") | |||
| 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 | ||