unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* Random Doc improvement
@ 2008-01-17 16:59 Stephen Uitti
  2008-02-11 22:29 ` Neil Jerram
  0 siblings, 1 reply; 2+ messages in thread
From: Stephen Uitti @ 2008-01-17 16:59 UTC (permalink / raw)
  To: bug-guile

I've been scratching my head over random numbers.

$ guile --version
Guile 1.8.1

and the Guile Ref i'm reading says:

This reference manual documents Guile, GNU's
Ubiquitous Intelligent Language for Extensions. This
is edition 1.1 corresponding to Guile 1.8.3.

Section 5.5.2.15 Random Number Generation

It really should say that (random) produces the same
list of numbers every time, unless a state is
specified.  And it should have an example showing use.
 Here's my example.

#!/usr/bin/guile -s
!#
;;; Usage: $ randlist.g

;;; If you don't do this, you get the same sequence
every time.
;;; This defines a seed state, which you must pass to
(random)
;;; every time.
;;; (gettimeofday) returns a pair, seconds and
microseconds.
;;; Add them for maximum randomness.
;;; For security applications, such as password
generation,
;;; i'd like more bits of seed.  Else an open source
password
;;; generator can be attacked by guessing the seed. 
If i just
;;; used seconds, that'd be easy.  Only 31,536,000
seconds in a year.
(define __rseed
  (seed->random-state (+ (car (gettimeofday))
			 (cdr (gettimeofday)))))

(define (randlist n max)
  (if (zero? n) '()
      (append (list (random max __rseed))
	      (randlist (- n 1) max))))

(display (randlist 20 100))
(newline)

Uhm, i don't need credit for this snippet.  Please use
it or something like it under any license.

Stephen Uitti
suitti@uitti.net



      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 





^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Random Doc improvement
  2008-01-17 16:59 Random Doc improvement Stephen Uitti
@ 2008-02-11 22:29 ` Neil Jerram
  0 siblings, 0 replies; 2+ messages in thread
From: Neil Jerram @ 2008-02-11 22:29 UTC (permalink / raw)
  To: Stephen Uitti; +Cc: bug-guile

Stephen Uitti <suitti@yahoo.com> writes:

> I've been scratching my head over random numbers.
[...]
> It really should say that (random) produces the same
> list of numbers every time, unless a state is
> specified.  And it should have an example showing use.

Thanks for pointing out that we could do with some more explanation
here.  I'm about to commit the addition below, loosely based on your
suggestions.  If you have any further comments, please let me know.

Regards,
        Neil


   Note that the initial value of `*random-state*' is the same every
time Guile starts up.  Therefore, if you don't pass a STATE parameter
to the above procedures, and you don't set `*random-state*' to
`(seed->random-state your-seed)', where `your-seed' is something that
_isn't_ the same every time, you'll get the same sequence of "random"
numbers on every run.

   For example, unless the relevant source code has changed, `(map
random (cdr (iota 30)))', if the first use of random numbers since
Guile started up, will always give:

     (map random (cdr (iota 19)))
     =>
     (0 1 1 2 2 2 1 2 6 7 10 0 5 3 12 5 5 12)

   To use the time of day as the random seed, you can use code like
this:

     (let ((time (gettimeofday)))
       (set! *random-state*
             (seed->random-state (+ (car time)
                                    (cdr time)))))

And then (depending on the time of day, of course):

     (map random (cdr (iota 19)))
     =>
     (0 0 1 0 2 4 5 4 5 5 9 3 10 1 8 3 14 17)

   For security applications, such as password generation, you should
use more bits of seed.  Otherwise an open source password generator
could be attacked by guessing the seed... but that's a subject for
another manual.





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-02-11 22:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-17 16:59 Random Doc improvement Stephen Uitti
2008-02-11 22:29 ` Neil Jerram

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).