unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Leo <sdl.web@gmail.com>
To: Juanma Barranquero <lekktu@gmail.com>
Cc: Lars Magne Ingebrigtsen <larsi@gnus.org>, 9118@debbugs.gnu.org
Subject: bug#9118: 23.3.50; Don't seed the RNG in message-unique-id
Date: Thu, 21 Jul 2011 23:58:46 +0800	[thread overview]
Message-ID: <m1aac7eibt.fsf@gmail.com> (raw)
In-Reply-To: <CAAeL0ST2yo83x0F0kH3zQP_7WMkjD1R8Gak=fe7Lvxo_CWG9mQ@mail.gmail.com> (Juanma Barranquero's message of "Thu, 21 Jul 2011 13:57:51 +0200")

On 2011-07-21 19:57 +0800, Juanma Barranquero wrote:
> Many systems (languages, etc.) have a facility to seed the RNG with a
> specific seed (saved from a previous run, for example), so you can
> have repeated runs with the exact same sequence of random numbers, if
> needed. That can be useful for simulation, testing, etc.
>
> AFAIK, the Emacs RNG does not allow that at the lisp level, so it
> would be better to automatically seed it and remove the many (random
> t) calls, which, as has been pointed out, can in fact be harmful for
> the quality of the generated random number series, if called
> repeatedly.

I agree completely.

How about something along these lines? (note: before this patch (random
1.0) is equivalent to (random most-positive-fixnum)).

=== modified file 'src/fns.c'
--- src/fns.c	2011-07-05 02:51:15 +0000
+++ src/fns.c	2011-07-21 15:47:30 +0000
@@ -69,15 +69,15 @@
 }
 
 DEFUN ("random", Frandom, Srandom, 0, 1, 0,
-       doc: /* Return a pseudo-random number.
+       doc: /* Return a non-negative pseudo-random number less than LIMIT.
 All integers representable in Lisp are equally likely.
   On most systems, this is 29 bits' worth.
-With positive integer LIMIT, return random number in interval [0,LIMIT).
-With argument t, set the random number seed from the current time and pid.
-Other values of LIMIT are ignored.  */)
+Optional argument LIMIT defaults to `most-positive-fixnum'.
+With argument t, set the random number seed from the current time
+  and pid and return nil.  */)
   (Lisp_Object limit)
 {
-  EMACS_INT val;
+  EMACS_INT val, denominator;
   Lisp_Object lispy_val;
 
   if (EQ (limit, Qt))
@@ -85,24 +85,28 @@
       EMACS_TIME t;
       EMACS_GET_TIME (t);
       seed_random (getpid () ^ EMACS_SECS (t) ^ EMACS_USECS (t));
+      return Qnil;
     }
 
-  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 ();
+  if (NILP (limit))
+    XSETINT (limit, MOST_POSITIVE_FIXNUM);
+
+  CHECK_NATNUM (limit);
+
+  if (XFASTINT (limit) == 0)
+    xsignal1 (Qargs_out_of_range, limit);
+
+  /* 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.  */
+  denominator = (INTMASK + 1) / XFASTINT (limit);
+  do
+    val = get_random () / denominator;
+  while (val >= XFASTINT (limit));
   XSETINT (lispy_val, val);
   return lispy_val;
 }
@@ -5009,4 +5013,5 @@
 void
 init_fns (void)
 {
+  Frandom (Qt);
 }






  reply	other threads:[~2011-07-21 15:58 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-18 16:42 bug#9118: 23.3.50; Don't seed the RNG in message-unique-id Leo
2011-07-19 14:57 ` Lars Magne Ingebrigtsen
2011-07-19 15:14   ` Leo
2011-07-19 15:24     ` Lars Magne Ingebrigtsen
2011-07-19 15:44       ` Leo
2011-07-19 15:49         ` Lars Magne Ingebrigtsen
2011-07-20  2:12           ` Leo
2011-07-20 20:28             ` Lars Magne Ingebrigtsen
2011-07-21  4:45               ` Leo
2011-07-21 11:57                 ` Juanma Barranquero
2011-07-21 15:58                   ` Leo [this message]
2011-07-31 15:39                     ` Lars Magne Ingebrigtsen
2011-07-31 16:02                       ` Leo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m1aac7eibt.fsf@gmail.com \
    --to=sdl.web@gmail.com \
    --cc=9118@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    --cc=lekktu@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).