bug-guile
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Random Doc improvement


From: Stephen Uitti
Subject: Random Doc improvement
Date: Thu, 17 Jan 2008 08:59:48 -0800 (PST)

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
address@hidden



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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]