Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!apple!sun-barr!lll-winken!uunet!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.prolog Subject: Re: random number generator Message-ID: <4682@goanna.cs.rmit.oz.au> Date: 31 Jan 91 09:00:54 GMT References: <27A20DBF.15393@ics.uci.edu> <4024@skye.ed.ac.uk> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 51 In article <4024@skye.ed.ac.uk>, ken@aiai.ed.ac.uk (Ken Johnson) writes: > The following is in the Prolog Library and (as far as I know!!) is > public domain. Please acknowledge ROK's authorship. The algorithm is > very clever. Oh dear. Did the version of RANDOM.PL I left at Edinburgh *really* omit the attribution? The algorithm used there for generating uniform random floats is algorithm AS 163 from the journal Applied Statistics. (In effect it is a 48-bit linear congruential method.) > random(N, I) :- > ( recorded(seed,[A0,A1,A2],Key) > -> erase(Key) > ; > A0 = 3172, A1 = 9814, A2 = 20125 > ), > ... > record(seed,[B0,B1,B2],_), > ... I'm quite sure *I* didn't leave it like that. The third argument of recorded/1 is NOT a "key". It's a "data base reference", "dbref" or "ref" for short. The FIRST argument is the "key". Further, the thing that is recorded is not a seed, it is a random state, and it always contains precisely three numbers, so a list is not appropriate. What's more, if you haven't modules, it's a good idea to choose key names that are unlikely to picked by users. Hence recorded('random state', 'random state'(A0,A1,A2), Ref) ... recorda('random state', 'random state'(B1,B1,B3), _) > % rand_perm(List, Perm) unifies Perm with a random permutation > % of List. What good this may be I'm not sure, and there's bound > % to be a more efficient way of doing it. Oh well. > rand_perm([], []). > rand_perm([H1|T1], [H2|T2]) :- > random([H1|T1], H2, T3), > rand_perm(T3, T2). I'm sure that comment can't be mine. There are *much* more efficient ways of doing it, and the Quintus library uses one of them. What's more, the operation is there because it is extremely useful; I found that most of the time that I was generating random numbers was to shuffle lists (generate random permuations), so it seemed as well to do it directly. (Think about generating test data.) -- The Marxists have merely _interpreted_ Marxism in various ways; the point, however, is to _change_ it. -- R. Hochhuth.