Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!satisfied.elf.com!news.mathworks.com!newshost.marcam.com!zip.eecs.umich.edu!umn.edu!epx.cis.umn.edu!jhgreve
From: jhgreve@epx.cis.umn.edu ()
Subject: Re: random numbers in Smalltalk - so much for email...
Message-ID: <D4J9wr.KKw@news.cis.umn.edu>
Sender: news@news.cis.umn.edu (Usenet News Administration)
Nntp-Posting-Host: epx.cis.umn.edu
Organization: University of Minnesota
References: <1734B9E45S86.JCOLTEN@vm1.spcs.umn.edu>
Date: Sat, 25 Feb 1995 02:06:36 GMT
Lines: 247

In article <1734B9E45S86.JCOLTEN@vm1.spcs.umn.edu>,
Jim Colten <JCOLTEN@vm1.spcs.umn.edu> wrote:
>Is there random number generator method in ST/V?  Have looked but didn't find
>one.
> 
>If there isn't, anyone have one?
> 
>Jim Colten
Hmm... so much for the email address i got from you at OTUG.
I'll check my outbound msg. file - i'm betting i typed it incorrectly :-(
(Although it hasn't come back as bounced yet...)

Anyway, here's the random stuff that I've pulled off of this group:
	John G.
	jhgreve@epx.cis.umn.edu
----------------------------------------------------------------

From jsutherland@BIX.com Sun Dec 18 10:57:39 CST 1994
Article: 15199 of comp.lang.smalltalk
Subject: The only random number generator you should use ...
Date: Wed, 14 Dec 1994 12:38:01
Organization: Galahad
Lines: 61
Message-ID: <3cndgp$2n9@news1.delphi.com>
NNTP-Posting-Host: bix.com

By popular demand, here is the essential information
on random number generators from Comm. of the ACM, Oct
1988, p. 1192.  Park and Miller in "Random number generators:
Good ones are hard to find" ...

1. Knuth recommends that we all look at the subroutine
(or class) library in every one of our organizations and
replace all random number generators with good ones.  "Don't
be too shocked by what you find!"

2. 50 textbooks were reviewed that had code for at least
one random number generator and most of the generators were
unsatisfactory.

3. The minimal standard is a multiplicative linear congruential
generator with multiplier 16807 and prime modulus 2^31-1.
Porting this type of generator is not trivial.

4. The generator in this paper should always be used unless you
can prove you have a better one.  You probably don't want to 
try unless you are a professional numerical analysis type
writing papers in the field.

5. A real version of the correct generator must be used
if maxint in smaller than 2^31-1.  It will be correct on any
system for which reals are represented with a 32-bit or
larger mantissa (including the sign bit).  First declare

var seed: real

then use,

function Random : real;

const
  a = 16897.0;
  m = 2147483647.0;
  q = 127773.0; (* m div a *)
  r = 2836.0;   (* m mod a *)
var
  lo, hi, test : read;
begin
  hi := Trunc(seed /q );
  lo := seed -q * hi;
  test := a * lo - r * hi
  if test > 0.0 then
    seed := test
  else
    seed := test + m;
  Random := seed /m
end;

Sorry for the Pascal, but I would run a test in Turbo Pascal
against the results from any Smalltalk implementation 
before I would trust the results.

Study the paper carefully before you implement (I wouldn't
use anyone else's random number generator).

Jeff Sutherland
Easel Corp.


From jsutherland@BIX.com Sun Jan  1 13:59:56 CST 1995
Article: 15526 of comp.lang.smalltalk
Newsgroups: comp.lang.smalltalk
Subject: Random Number Generator (repost) - the only one to use ...
Date: Fri, 30 Dec 1994 11:03:59
Organization: Galahad
Lines: 146
Message-ID: <3e1as6$req@news1.delphi.com>
NNTP-Posting-Host: bix.com

After posting Enfin Smalltalk code for the Park & Miller random number
generator, I received some excellent feedback on how to make it simpler to
port to any Smalltalk variant.

I used the new Synchronicity code generator, set it to generate compliant
code, and turned off all generation of methods to support domains,
relationships, and change propogation.  This strips the code of things you
would want in a commercial application like MVC or event support. 
Synchronicity stills does about 50% of the work.

What's left should be self explanatory.  Let me know if it runs
straightaway in Visual Smalltalk, Visual Age, Visual Works, or QKS.

To test, type

Random validate.  (Result should be 'OK') 

I would really like to know if this breaks on a Pentium.  It is quite
possible that it will trigger the Pentium bug for rounding error.  If it
doesn't break the first time, write your own validate instance method,
generate a set of random seeds, and validate starting with each random seed
until it breaks.  Can someone test it?

Object subclass: #Random
instanceVariableNames: '
    seed
    hi
    lo
    test
'
classVariableNames: '
    a
    m
    q
    r
'
poolDictionaries: '
' !


!Random class methods !


a
	"Modeling Tool generated method"
	^a.
!




initialize
	a := 16807.
	m:= 2147483647.
	q:= m//a.
	r:= m\\a.
!"end initialize "


m
	"Modeling Tool generated method"
	^m.
!


new
	"Modeling Tool generated method"
	^super new initialize.
!


q
	"Modeling Tool generated method"
	^q.
!


r
	"Modeling Tool generated method"
	^r.
!


validate
	|num|
	num:=Random new.
	num seed: 1.
	10000 timesRepeat: [
	num generate.
	].
	(1043618065 = num seed) 
		ifTrue: [ ^'OK.' ]
		ifFalse: [ ^'Bad result. Fix this generator by referring to Park &
Miller, Comm ACM 31:10:1192-1201, 1988.' ].
	
!


!


!Random methods !


generate
     "Real Version 2 from Park and Miller, 1988"
     |hi lo test|
	  
	hi := (self seed) quo: q.
	lo := (self seed) - (q * hi).
	test := (a * lo) - (r * hi).
	test > 0.0 
		ifTrue: [ self seed: test. ]  
		ifFalse: [ self seed: test + m. ].
	^seed / m.

!


initialize
	"Modeling Tool generated method"
	seed := Float new.
	hi := Float new.
	lo := Float new.
	test := Float new.

!


seed
	"Modeling Tool generated method"
	^seed.
!


seed: aSeed
	"Modeling Tool generated method"
	seed == aSeed ifTrue: [^seed].
	seed := aSeed.
!


!

Jeff Sutherland		jsutherland@easel.com
Easel Corp.                                  (via Galahad bix blinker)


