]> git.eshelyaron.com Git - emacs.git/commitdiff
Better seed support for (random).
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 1 Sep 2012 01:04:26 +0000 (18:04 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 1 Sep 2012 01:04:26 +0000 (18:04 -0700)
* doc/lispref/numbers.texi (Random Numbers): Document new behavior of
the calls (random) and (random STRING).
* etc/NEWS: Document new behavior of (random), (random "string").
* lisp/play/5x5.el, lisp/play/animate.el, lisp/play/cookie1.el:
* lisp/play/dissociate.el, lisp/play/doctor.el, lisp/play/dunnet.el:
* lisp/play/gomoku.el, lisp/play/landmark.el, lisp/play/mpuz.el:
* lisp/play/tetris.el, lisp/play/zone.el:
* lisp/calc/calc-comb.el (math-init-random-base):
* lisp/play/blackbox.el (bb-init-board):
* lisp/play/life.el (life):
* lisp/server.el (server-use-tcp):
* lisp/type-break.el (type-break):
Remove unnecessary call to (random t).
* lisp/net/sasl.el (sasl-unique-id-function):
Change (random t) to (random), now that the latter is more random.
* lisp/play/life.el (life-initialized): Remove no-longer-needed var.
* lisp/gnus/gnus-sync.el (gnus-sync-lesync-setup):
* lisp/gnus/message.el (message-canlock-generate, message-unique-id):
Change (random t) to (random), now that the latter is more random.
* lisp/org/org-id.el (org-id-uuid):
Change (random t) to (random), now that the latter is more random.
* src/emacs.c (main): Call init_random.
* src/fns.c (Frandom): Set the seed from a string argument, if given.
Remove long-obsolete Gentzel cruft.
* src/lisp.h, src/sysdep.c (seed_random): Now takes address and size, not long.
(init_random): New function.

33 files changed:
doc/lispref/ChangeLog
doc/lispref/numbers.texi
etc/ChangeLog
etc/NEWS
lisp/ChangeLog
lisp/allout-widgets.el
lisp/calc/calc-comb.el
lisp/gnus/ChangeLog
lisp/gnus/gnus-sync.el
lisp/gnus/message.el
lisp/net/sasl.el
lisp/org/ChangeLog
lisp/org/org-id.el
lisp/play/5x5.el
lisp/play/animate.el
lisp/play/blackbox.el
lisp/play/cookie1.el
lisp/play/dissociate.el
lisp/play/doctor.el
lisp/play/dunnet.el
lisp/play/gomoku.el
lisp/play/landmark.el
lisp/play/life.el
lisp/play/mpuz.el
lisp/play/tetris.el
lisp/play/zone.el
lisp/server.el
lisp/type-break.el
src/ChangeLog
src/emacs.c
src/fns.c
src/lisp.h
src/sysdep.c

index c014c0fce45c6caa901a988a2d3674c08cc3d5dc..30169d6b7a99203189548888cb313b67fe5f9e6c 100644 (file)
@@ -1,3 +1,9 @@
+2012-09-01  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Better seed support for (random).
+       * numbers.texi (Random Numbers): Document new behavior of
+       the calls (random) and (random STRING).
+
 2012-08-21  Martin Rudalics  <rudalics@gmx.at>
 
        * windows.texi (Window Point): Document recent changes in
index ce0716f39ef2ef055084c9e95d913a694943d4d8..17f3ee099bdcd007525a866ee1cbd96868680b3e 100644 (file)
@@ -1199,30 +1199,32 @@ numbers are not truly random, but they have certain properties that
 mimic a random series.  For example, all possible values occur equally
 often in a pseudo-random series.
 
-In Emacs, pseudo-random numbers are generated from a ``seed'' number.
+In Emacs, pseudo-random numbers are generated from a ``seed''.
 Starting from any given seed, the @code{random} function always
-generates the same sequence of numbers.  Emacs always starts with the
-same seed value, so the sequence of values of @code{random} is actually
-the same in each Emacs run!  For example, in one operating system, the
-first call to @code{(random)} after you start Emacs always returns
-@minus{}1457731, and the second one always returns @minus{}7692030.  This
-repeatability is helpful for debugging.
-
-If you want random numbers that don't always come out the same, execute
-@code{(random t)}.  This chooses a new seed based on the current time of
-day and on Emacs's process @acronym{ID} number.
+generates the same sequence of numbers.  Emacs typically starts with a
+different seed each time, so the sequence of values of @code{random}
+typically differs in each Emacs run.
+
+Sometimes you want the random number sequence to be repeatable.  For
+example, when debugging a program whose behavior depends on the random
+number sequence, it is helpful to get the same behavior in each
+program run.  To make the sequence repeat, execute @code{(random "")}.
+This sets the seed to a constant value for your particular Emacs
+executable (though it may differ for other Emacs builds).  You can use
+other strings to choose various seed values.
 
 @defun random &optional limit
 This function returns a pseudo-random integer.  Repeated calls return a
 series of pseudo-random integers.
 
 If @var{limit} is a positive integer, the value is chosen to be
-nonnegative and less than @var{limit}.
+nonnegative and less than @var{limit}.  Otherwise, the value
+might be any integer representable in Lisp.
 
 If @var{limit} is @code{t}, it means to choose a new seed based on the
 current time of day and on Emacs's process @acronym{ID} number.
 
-On some machines, any integer representable in Lisp may be the result
-of @code{random}.  On other machines, the result can never be larger
-than a certain maximum or less than a certain (negative) minimum.
+If @var{limit} is a string, it means to choose a new seed based on the
+string's contents.
+
 @end defun
index 11aba88c6a11c78fb7f524dddf9298eb8b9b3007..f3f244c7e39c2bba8bb47a1701fcb293aa652f84 100644 (file)
@@ -1,3 +1,8 @@
+2012-09-01  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Better seeds for (random).
+       * NEWS: Document new behavior of (random), (random "string").
+
 2012-08-28  Andreas Schwab  <schwab@linux-m68k.org>
 
        * charsets/MULE-ethiopic.map: Fix typo in comment.
index 145217446701473d17cf68aad4b5d9837c66f37a..58298a9a43728dd8c63c91df788723b7fac7f3c6 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -527,6 +527,11 @@ inefficiency, and not namespace-clean.
 \f
 * Incompatible Lisp Changes in Emacs 24.3
 
+** (random) by default now returns a different random sequence in
+every Emacs run.  Use (random S), where S is a string, to set the
+random seed to a value based on S, in order to get a repeatable
+sequence in later calls.
+
 ** The function `x-select-font' can return a font spec, instead of a
 font name as a string.  Whether it returns a font spec or a font name
 depends on the graphical library.
index ce4493d4c76e090e390e3b13d2742efd4389aecc..68bb8e06e8760988ab4da4a72a9774208bed29f9 100644 (file)
@@ -1,3 +1,19 @@
+2012-09-01  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Better seed support for (random).
+       * play/5x5.el, play/animate.el, play/cookie1.el, play/dissociate.el:
+       * play/doctor.el, play/dunnet.el, play/gomoku.el, play/landmark.el:
+       * play/mpuz.el, play/tetris.el, play/zone.el:
+       * calc/calc-comb.el (math-init-random-base):
+       * play/blackbox.el (bb-init-board):
+       * play/life.el (life):
+       * server.el (server-use-tcp):
+       * type-break.el (type-break):
+       Remove unnecessary call to (random t).
+       * net/sasl.el (sasl-unique-id-function):
+       Change (random t) to (random), now that the latter is more random.
+       * play/life.el (life-initialized): Remove no-longer-needed var.
+
 2012-08-31  Alp Aker  <alp.tekin.aker@gmail.com>
 
        * window.el (switch-to-prev-buffer, switch-to-next-buffer):
index 962a8fb557ac6addaabae1acef0d6605ab882840..c5790603d11fe96fbd0b761d8221190c26015381 100644 (file)
@@ -1374,7 +1374,6 @@ FROM and TO must be in increasing order, as must be the pairs in RANGES."
 ;;              (time-trial
 ;;               '(let ((size 10000)
 ;;                      doing)
-;;                  (random t)
 ;;                  (dotimes (count size)
 ;;                    (setq doing (random size))
 ;;                    (funcall try doing (+ doing (random 5)))
index 199bbf0ae35a3c2ee3893c3ce3737c37cccd40c1..431ea18f5802474b4d8628080051b81015700469 100644 (file)
@@ -77,7 +77,7 @@
      4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973
      4987 4993 4999 5003])
 
-;; The variable math-prime-factors-finished is set by calcFunc-prfac to 
+;; The variable math-prime-factors-finished is set by calcFunc-prfac to
 ;; indicate whether factoring is complete, and used by calcFunc-factors,
 ;; calcFunc-totient and calcFunc-moebius.
 (defvar math-prime-factors-finished)
       (while (<= (length math-stirling-local-cache) n)
        (let ((i (1- (length math-stirling-local-cache)))
              row)
-         (setq math-stirling-local-cache 
-                (vconcat math-stirling-local-cache 
+         (setq math-stirling-local-cache
+                (vconcat math-stirling-local-cache
                          (make-vector (length math-stirling-local-cache) nil)))
          (aset math-stirling-cache k math-stirling-local-cache)
          (while (< (setq i (1+ i)) (length math-stirling-local-cache))
        (let ((i 200))
          (while (> (setq i (1- i)) 0)
            (math-random-base))))
-    (random t)
     (setq var-RandSeed nil
          math-random-cache nil
          math-random-shift -4)  ; assume RAND_MAX >= 16383
          (i (/ (+ n slop) 3))
          (rnum 0))
     (while (> i 0)
-      (setq rnum 
+      (setq rnum
             (math-add
              (math-random-three-digit-number)
              (math-mul rnum 1000)))
                                 (setq sum (%
                                            (+
                                             sum
-                                            (calcFunc-mod 
+                                            (calcFunc-mod
                                              q 1000000))
                                            111111))
-                                (setq q 
-                                      (math-quotient 
+                                (setq q
+                                      (math-quotient
                                        q 1000000)))
                              (cond ((= (% sum 3) 0) '(nil 3))
                                    ((= (% sum 7) 0) '(nil 7))
index b3aeb96e9fe2385d6216b33141075216872fda26..a4e3d9bde2beb22dac818f3ce52724ebaf9d5030 100644 (file)
@@ -1,3 +1,10 @@
+2012-09-01  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Better seeds for (random).
+       * gnus-sync.el (gnus-sync-lesync-setup):
+       * message.el (message-canlock-generate, message-unique-id):
+       Change (random t) to (random), now that the latter is more random.
+
 2012-08-31  Dave Abrahams  <dave@boostpro.com>
 
        * auth-source.el (auth-sources): Fix macos keychain access.
index 15d94810c3afe93ef915f3814b0900a27e5deec7..ca8662ff9361a153a311a54d4f861eb35e838589 100644 (file)
@@ -225,7 +225,7 @@ When SALT is nil, a random one will be generated using `random'."
          (security-object (concat url "/_security"))
          (user-record `((names . [,user]) (roles . [])))
          (couch-user-name (format "org.couchdb.user:%s" user))
-         (salt (or salt (sha1 (format "%s" (random t)))))
+         (salt (or salt (sha1 (format "%s" (random)))))
          (couch-user-record
           `((_id . ,couch-user-name)
             (type . user)
index 8fd89b1742ceb3dbd7b47653b30d3712d557d646..18088423eb06c6fa6233b1f36d9b2e54976dba1d 100644 (file)
@@ -4820,9 +4820,7 @@ Do not use this for anything important, it is cryptographically weak."
   (require 'sha1)
   (let (sha1-maximum-internal-length)
     (sha1 (concat (message-unique-id)
-                 (format "%x%x%x" (random)
-                         (progn (random t) (random))
-                         (random))
+                 (format "%x%x%x" (random) (random) (random))
                  (prin1-to-string (recent-keys))
                  (prin1-to-string (garbage-collect))))))
 
@@ -5525,7 +5523,6 @@ In posting styles use `(\"Expires\" (make-expires-date 30))'."
 ;; You might for example insert a "." somewhere (not next to another dot
 ;; or string boundary), or modify the "fsf" string.
 (defun message-unique-id ()
-  (random t)
   ;; Don't use microseconds from (current-time), they may be unsupported.
   ;; Instead we use this randomly inited counter.
   (setq message-unique-id-char
index c6e95970f3d9a20f2fde181066c8696b69ed422b..4e759a4e6b28039d08804f79d4d2842d4391002c 100644 (file)
@@ -183,7 +183,7 @@ It contain at least 64 bits of entropy."
   ;; Don't use microseconds from (current-time), they may be unsupported.
   ;; Instead we use this randomly inited counter.
   (setq sasl-unique-id-char
-       (% (1+ (or sasl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
+       (% (1+ (or sasl-unique-id-char (logand (random) (1- (lsh 1 20)))))
           ;; (current-time) returns 16-bit ints,
           ;; and 2^16*25 just fits into 4 digits i base 36.
           (* 25 25)))
index cf9c32006accea83c682472d36cdfec669eeb205..152af5f43edfe5b95909e157f2cf879486082931 100644 (file)
@@ -1,3 +1,9 @@
+2012-09-01  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Better seed support for (random).
+       * org-id.el (org-id-uuid):
+       Change (random t) to (random), now that the latter is more random.
+
 2012-07-29  Paul Eggert  <eggert@cs.ucla.edu>
 
        Don't use the abbreviation "win" to refer to Windows (Bug#10421).
index 55e826f3ae69b433e866f0412dab0cc06ebffcb5..a93f804946fe91c7401b14bbaef95116870b02b2 100644 (file)
@@ -318,7 +318,7 @@ So a typical ID could look like \"Org:4nd91V40HI\"."
 (defun org-id-uuid ()
   "Return string with random (version 4) UUID."
   (let ((rnd (md5 (format "%s%s%s%s%s%s%s"
-                         (random t)
+                         (random)
                          (current-time)
                          (user-uid)
                          (emacs-pid)
index e6df0df828213a283474923c0c296b1b927912a6..abc78cd495c93381f5a80fe332b0a5e719b7a17e 100644 (file)
@@ -953,8 +953,6 @@ lest."
       (y-or-n-p prompt)
     t))
 
-(random t)
-
 (provide '5x5)
 
 ;;; 5x5.el ends here
index 9919600202f246e1bcb8d94dca0e67be5cf94ca4..2398a7b89c787cffaaca0a2324ef6e5353ca46cb 100644 (file)
@@ -201,8 +201,6 @@ the buffer *Birthday-Present-for-Name*."
   (animate-string "my sunshine" 18 34)
   (animate-string "to stay!" 19 34))
 
-(random t)
-
 (provide 'animate)
 
 ;;; animate.el ends here
index eb2d784c8e523c1f8b28f26f97cc547b083a3955..db2e18188e5cb55c602c503208f6cec5543080ee 100644 (file)
@@ -93,7 +93,7 @@
   (define-key map (vector 'remap oldfun) newfun))
 
 
-(defvar blackbox-mode-map 
+(defvar blackbox-mode-map
   (let ((map (make-keymap)))
     (suppress-keymap map t)
     (blackbox-redefine-key map 'backward-char 'bb-left)
@@ -257,7 +257,6 @@ a reflection."
   (bb-goto (cons bb-x bb-y)))
 
 (defun bb-init-board (num-balls)
-  (random t)
   (let (board pos)
     (while (>= (setq num-balls (1- num-balls)) 0)
       (while
index cbf29a26a713bf1bd7c4d95c2f068d0d5d8a1d92..dbd6e89347355eeda3b658514f5b820b691c485e 100644 (file)
@@ -53,9 +53,6 @@
 
 ;;; Code:
 
-; Randomize the seed in the random number generator.
-(random t)
-
 (defconst cookie-delimiter "\n%%\n\\|\n%\n\\|\0"
   "Delimiter used to separate cookie file entries.")
 
index 8f9f8cea24fb4d8fd7d5bf8fc5cc0c045534bb3e..238b2a86c171683094f5c279ceca243b5261013d 100644 (file)
@@ -94,8 +94,6 @@ Default is 2."
                 (funcall search-function overlap opoint t))))))
       (sit-for 0))))
 
-(random t)
-
 (provide 'dissociate)
 
 ;;; dissociate.el ends here
index fdae2ec93261f242a64a58de03a23855892199cc..57dbb1452f30884cab2449e04d613fd771acac89 100644 (file)
@@ -1620,8 +1620,6 @@ Hack on previous word, setting global variable DOCTOR-OWNER to correct result."
 
 (defun doctor-chat () (doctor-type (doc$ doctor--chatlst)))
 
-(random t)
-
 (provide 'doctor)
 
 ;;; doctor.el ends here
index 22a2642f7512b746d3ce0790cb297d1f57155cbd..2d62b800ef4631364ebd91521e4f97c0a39ac06a 100644 (file)
@@ -3010,7 +3010,6 @@ drwxr-xr-x  3 root     staff          2048 Jan 1 1970 ..")
     (dun-uexit nil)))
 
 
-(random t)
 (setq tloc (+ 60 (random 18)))
 (dun-replace dun-room-objects tloc
             (append (nth tloc dun-room-objects) (list 18)))
index 6d73e2ccb65daa035bf51a814798a6be51b3e6aa..3dba99d98d5e3d5cb26ee94ce24bdd5120e68267 100644 (file)
@@ -1197,8 +1197,6 @@ If the game is finished, this command requests for another game."
   (move-to-column (+ gomoku-x-offset
                     (* gomoku-square-width (1- gomoku-board-width)))))
 
-(random t)
-
 (provide 'gomoku)
 
 ;;; gomoku.el ends here
index e9f555093dbf86fa4694ff85af10620a704d6009..e103249da49581fdd7734f08210a4a662267e619 100644 (file)
@@ -1683,8 +1683,6 @@ Use \\[describe-mode] for more info."
 ;;;allout-layout: (0 : -1 -1 0)
 ;;;End:
 
-(random t)
-
 (provide 'landmark)
 
 ;;; landmark.el ends here
index e9133f84862a3caa9f069a2a7276af55772a1fa3..87ec0226af5bd9bd50ad91a9285bd072cd12767d 100644 (file)
 ;; Sadly, mode-line-format won't display numbers.
 (defvar life-generation-string nil)
 
-(defvar life-initialized nil
-  "Non-nil if `life' has been run at least once.")
-
 ;;;###autoload
 (defun life (&optional sleeptime)
   "Run Conway's Life simulation.
@@ -121,9 +118,6 @@ The starting pattern is randomly selected.  Prefix arg (optional first
 arg non-nil from a program) is the number of seconds to sleep between
 generations (this defaults to 1)."
   (interactive "p")
-  (or life-initialized
-      (random t))
-  (setq life-initialized t)
   (or sleeptime (setq sleeptime 1))
   (life-setup)
   (catch 'life-exit
index c0c8803f8963decba10f7f958e16cdb4f62bd7cc..e16bb2f1bde3b565f62f82c77266632db2f9ea9a 100644 (file)
@@ -35,8 +35,6 @@
   :prefix "mpuz-"
   :group 'games)
 
-(random t)                             ; randomize
-
 (defcustom mpuz-silent 'error
   "Set this to nil if you want dings on inputs.
 The value t means never ding, and `error' means only ding on wrong input."
index b811a21605b4f0f4c259b1d26c773ea1788046fd..9cc33304589e0ee88fdbef33f98e81bd54094ef0 100644 (file)
@@ -635,8 +635,6 @@ tetris-mode keybindings:
   (tetris-mode)
   (tetris-start-game))
 
-(random t)
-
 (provide 'tetris)
 
 ;;; tetris.el ends here
index 34e21193437642dea738f18d826d3f17259d1947..1cfc6c59987850fe5948467e5f347f032dbe1d26 100644 (file)
@@ -675,8 +675,6 @@ If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
       (kill-buffer nil))))
 
 
-(random t)
-
 ;;;;;;;;;;;;;;;
 (provide 'zone)
 
index 6d34df351ca0ddafe27a6c9f8889ae2e75b81f50..4fd55bcf6d191e2cda15d86cbcee63b78ea6bba0 100644 (file)
@@ -94,7 +94,6 @@
              (setq val t)
              (unless load-in-progress
                (message "Local sockets unsupported, using TCP sockets")))
-           (when val (random t))
            (set-default sym val))
   :group 'server
   :type 'boolean
index f0c81125f0c0fe7c222154784ac0e5a356130d50..8a95508d9393a2b4000652a0b135e24add2c404c 100644 (file)
@@ -577,7 +577,6 @@ as per the function `type-break-schedule'."
         (unless type-break-terse-messages
           (message "Press any key to resume from typing break."))
 
-        (random t)
         (let* ((len (length type-break-demo-functions))
                (idx (random len))
                (fn (nth idx type-break-demo-functions)))
index 4db48bbb96931a9b1d8a0cb8b084e0c21dec5bc6..ad96c948466084a9f00005d2c59632e767d340f5 100644 (file)
@@ -1,3 +1,12 @@
+2012-09-01  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Better seed support for (random).
+       * emacs.c (main): Call init_random.
+       * fns.c (Frandom): Set the seed from a string argument, if given.
+       Remove long-obsolete Gentzel cruft.
+       * lisp.h, sysdep.c (seed_random): Now takes address and size, not long.
+       (init_random): New function.
+
 2012-08-31  Dmitry Antipov  <dmantipov@yandex.ru>
 
        Remove mark_ttys function and fix tty_display_info initialization.
index 7ff5c43dbeab61de8f7454019aa663e29de50a47..842546461b64b4430d16eb2022aeac55e3b72799 100644 (file)
@@ -1281,6 +1281,7 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
   init_data ();
   init_atimer ();
   running_asynch_code = 0;
+  init_random ();
 
   no_loadup
     = argmatch (argv, argc, "-nl", "--no-loadup", 6, NULL, &skip_args);
index f6acdcada3f5aed996452bb5cc1cdc3911f36ccc..2dee8515799c08af8137a008ef655a1a7c65ceb5 100644 (file)
--- a/src/fns.c
+++ b/src/fns.c
@@ -74,32 +74,16 @@ Other values of LIMIT are ignored.  */)
   (Lisp_Object limit)
 {
   EMACS_INT val;
-  Lisp_Object lispy_val;
 
   if (EQ (limit, Qt))
-    {
-      EMACS_TIME t = current_emacs_time ();
-      seed_random (getpid () ^ EMACS_SECS (t) ^ EMACS_NSECS (t));
-    }
+    init_random ();
+  else if (STRINGP (limit))
+    seed_random (SSDATA (limit), SBYTES (limit));
 
+  val = get_random ();
   if (NATNUMP (limit) && XFASTINT (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.  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 n; discarding
-        these values eliminates the bias that would otherwise appear
-        when using a large n.  */
-      EMACS_INT denominator = (INTMASK + 1) / XFASTINT (limit);
-      do
-       val = get_random () / denominator;
-      while (val >= XFASTINT (limit));
-    }
-  else
-    val = get_random ();
-  XSETINT (lispy_val, val);
-  return lispy_val;
+    val %= XFASTINT (limit);
+  return make_number (val);
 }
 \f
 /* Heuristic on how many iterations of a tight loop can be safely done
index 80c49703f52e69f675c5b84815c00ae2d6473656..2815a2ae3256a280a7db46f44c46598db5aad5d2 100644 (file)
@@ -3405,7 +3405,8 @@ extern void child_setup_tty (int);
 extern void setup_pty (int);
 extern int set_window_size (int, int, int);
 extern EMACS_INT get_random (void);
-extern void seed_random (long);
+extern void seed_random (void *, ptrdiff_t);
+extern void init_random (void);
 extern int emacs_open (const char *, int, int);
 extern int emacs_close (int);
 extern ptrdiff_t emacs_read (int, char *, ptrdiff_t);
index 183ee00522766e61bd01b30a34b37cba89d36bb8..edd54de84d4c943da0f6a3a28d950cfc6097ab80 100644 (file)
@@ -1765,19 +1765,37 @@ init_signals (void)
 #endif /* !RAND_BITS */
 
 void
-seed_random (long int arg)
+seed_random (void *seed, ptrdiff_t seed_size)
 {
+#if defined HAVE_RANDOM || ! defined HAV_LRAND48
+  unsigned int arg = 0;
+#else
+  long int arg = 0;
+#endif
+  unsigned char *argp = (unsigned char *) &arg;
+  unsigned char *seedp = seed;
+  ptrdiff_t i;
+  for (i = 0; i < seed_size; i++)
+    argp[i % sizeof arg] ^= seedp[i];
 #ifdef HAVE_RANDOM
-  srandom ((unsigned int)arg);
+  srandom (arg);
 #else
 # ifdef HAVE_LRAND48
   srand48 (arg);
 # else
-  srand ((unsigned int)arg);
+  srand (arg);
 # endif
 #endif
 }
 
+void
+init_random (void)
+{
+  EMACS_TIME t = current_emacs_time ();
+  uintmax_t v = getpid () ^ EMACS_SECS (t) ^ EMACS_NSECS (t);
+  seed_random (&v, sizeof v);
+}
+
 /*
  * Return a nonnegative random integer out of whatever we've got.
  * It contains enough bits to make a random (signed) Emacs fixnum.