From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Neil Jerram Newsgroups: gmane.lisp.guile.bugs Subject: Re: Random Doc improvement Date: Mon, 11 Feb 2008 22:29:43 +0000 Message-ID: <87zlu7xh2g.fsf@ossau.uklinux.net> References: <998452.26392.qm@web34707.mail.mud.yahoo.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1202768999 9651 80.91.229.12 (11 Feb 2008 22:29:59 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 11 Feb 2008 22:29:59 +0000 (UTC) Cc: bug-guile@gnu.org To: Stephen Uitti Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Mon Feb 11 23:30:22 2008 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1JOhAK-0000Zj-1d for guile-bugs@m.gmane.org; Mon, 11 Feb 2008 23:30:20 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JOh9q-0006zA-Kh for guile-bugs@m.gmane.org; Mon, 11 Feb 2008 17:29:50 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JOh9m-0006yz-Qs for bug-guile@gnu.org; Mon, 11 Feb 2008 17:29:46 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JOh9m-0006ye-B1 for bug-guile@gnu.org; Mon, 11 Feb 2008 17:29:46 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JOh9m-0006yY-0Z for bug-guile@gnu.org; Mon, 11 Feb 2008 17:29:46 -0500 Original-Received: from mail3.uklinux.net ([80.84.72.33]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1JOh9l-0005wA-Lm for bug-guile@gnu.org; Mon, 11 Feb 2008 17:29:45 -0500 Original-Received: from arudy (host86-145-183-175.range86-145.btcentralplus.com [86.145.183.175]) by mail3.uklinux.net (Postfix) with ESMTP id A78361F69CF; Mon, 11 Feb 2008 22:29:44 +0000 (GMT) Original-Received: from laruns (laruns [192.168.0.10]) by arudy (Postfix) with ESMTP id 2FC563800A; Mon, 11 Feb 2008 22:29:44 +0000 (GMT) In-Reply-To: <998452.26392.qm@web34707.mail.mud.yahoo.com> (Stephen Uitti's message of "Thu, 17 Jan 2008 08:59:48 -0800 (PST)") User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) X-detected-kernel: by monty-python.gnu.org: Linux 2.4-2.6 X-BeenThere: bug-guile@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.bugs:3782 Archived-At: Stephen Uitti 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.