--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/RandomGNUSmalltalk.st Fri Mar 02 17:06:06 2018 +0100
@@ -0,0 +1,263 @@
+"
+======================================================================
+|
+| Copyright (C) 1988, 1989 Free Software Foundation, Inc.
+| Written by Steve Byrne.
+|
+| This file is part of GNU Smalltalk.
+|
+| GNU Smalltalk is free software; you can redistribute it and/or modify it
+| under the terms of the GNU General Public License as published by the Free
+| Software Foundation; either version 1, or (at your option) any later version.
+|
+| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
+| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+| details.
+|
+| You should have received a copy of the GNU General Public License along with
+| GNU Smalltalk; see the file LICENSE. If not, write to the Free Software
+| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+|
+======================================================================
+
+see notice in (Random>>documentation)
+"
+"{ Package: 'stx:libbasic2' }"
+
+"{ NameSpace: Smalltalk }"
+
+Random subclass:#RandomGNUSmalltalk
+ instanceVariableNames:'increment multiplier modulus'
+ classVariableNames:'SharedGenerator'
+ poolDictionaries:''
+ category:'Magnitude-Numbers-Random'
+!
+
+!RandomGNUSmalltalk class methodsFor:'documentation'!
+
+copyright
+"
+======================================================================
+|
+| Copyright (C) 1988, 1989 Free Software Foundation, Inc.
+| Written by Steve Byrne.
+|
+| This file is part of GNU Smalltalk.
+|
+| GNU Smalltalk is free software; you can redistribute it and/or modify it
+| under the terms of the GNU General Public License as published by the Free
+| Software Foundation; either version 1, or (at your option) any later version.
+|
+| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
+| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+| details.
+|
+| You should have received a copy of the GNU General Public License along with
+| GNU Smalltalk; see the file LICENSE. If not, write to the Free Software
+| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+|
+======================================================================
+
+see notice in (Random>>documentation)
+"
+!
+
+documentation
+"
+ WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
+ =======================================================================================
+ DO NOT USE THIS GENERATOR FOR CRYPTOGRAPHY OR OTHER SECURITY RELATED WORK,
+ because linear congruential generators are predictable and can be broken easily!!
+
+ Smalltalk/X includes a better generator named RandomGenerator,
+ which uses the best available generator of the system
+ (either OS-random, /dev/random or as a less dangerous fallback, an RC4-based random generator).
+ Please use that one.
+ =======================================================================================
+
+ A simple random numbers - thanks to Steves GNU Smalltalk
+
+ This implements a linear congruential maximum period random number generator
+ which passes the spectral test for randomness for dimensions 2 3 4 5 6.
+
+ Notice: although being included here,
+ this file is NOT covered by the ST/X license, but by
+ the FSF copyLeft (see copyright method).
+
+ You can redistribute it under the terms stated there ...
+ Also, the price you pay for ST/X does not include a charge for
+ this file - it has to be considered as a separate piece of
+ software, which can be copied and given away without any
+ restriction from my (CG) side.
+
+ [author:]
+ Steve Byrne
+ Claus Gittinger
+
+ [see also:]
+ http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf
+ RandomTT800 - a new random generator
+ RandomParkMiller - another new random generator
+"
+!
+
+examples
+"
+ [exBegin]
+ |rnd|
+
+ rnd := RandomGNUSmalltalk new.
+ 10 timesRepeat:[
+ Transcript showCR:(rnd next)
+ ]
+ [exEnd]
+
+ rolling a dice:
+ [exBegin]
+ |rnd|
+
+ rnd := RandomGNUSmalltalk new.
+ 10 timesRepeat:[
+ Transcript showCR:(rnd nextIntegerBetween:1 and:6)
+ ]
+ [exEnd]
+"
+! !
+
+!RandomGNUSmalltalk methodsFor:'accessing-reading'!
+
+maxInteger
+ ^ modulus
+!
+
+next
+ "return the next random number in the range ]0..1["
+
+ self step.
+ ^ seed / modulus asFloat
+
+ "
+ |r|
+ r := Random new.
+ Transcript showCR:r next.
+ Transcript showCR:r next.
+ Transcript showCR:r next.
+ Transcript showCR:r next.
+ "
+
+ "Modified: 1.4.1997 / 22:44:46 / cg"
+!
+
+nextBoolean
+ "return true or false by random"
+
+ self step.
+ ^ seed < (modulus // 2)
+
+ "
+ |r|
+ r := Random new.
+ Transcript showCR:r nextBoolean.
+ Transcript showCR:r nextBoolean.
+ Transcript showCR:r nextBoolean.
+ Transcript showCR:r nextBoolean.
+ "
+
+ "
+ |r bag|
+ r := Random new.
+ bag := Bag new.
+ 1000000 timesRepeat:[
+ bag add:(r nextBoolean).
+ ].
+ Transcript showCR:bag contents
+ "
+
+ "Modified: / 22-10-2008 / 15:17:57 / cg"
+!
+
+nextInteger
+ "return the next integral random number,
+ in the range 0 .. modulus (which is less than 16r3FFFFFFF).
+ From Sedgewick's 'Algorithms', based on Lehmer's method.
+
+ Take care, this returns an even number after each odd number!!"
+
+ self step.
+ ^ seed
+
+ "
+ |r|
+ r := Random new.
+ Transcript showCR:r nextInteger.
+ Transcript showCR:r nextInteger.
+ Transcript showCR:r nextInteger.
+ Transcript showCR:r nextInteger.
+ "
+
+ "Modified: 1.4.1997 / 22:42:53 / cg"
+! !
+
+!RandomGNUSmalltalk methodsFor:'private'!
+
+setSeed:seedValue
+ "set the initial seed and intialite the PRNG parameters.
+ These numbers implement a maximum period generator which passes
+ the spectral test for randomness for dimensions 2 3 4 5 6 and
+ the product does not overflow 2 raisedTo:29.
+
+ These numbers are carefully choosen, so don't change them,
+ unless you know what you are doing!!"
+
+ modulus := 244944 " 244957 " .
+ multiplier := 1597.
+ increment := 51749.
+ seed := seedValue \\ modulus.
+
+ self step.
+
+ "Modified: / 12-11-1999 / 17:50:52 / stefan"
+ "Created: / 26-05-2007 / 21:25:10 / cg"
+!
+
+setSeed:seedValue
+ "set the initial seed and intialite the PRNG parameters.
+ These numbers implement a maximum period generator which passes
+ the spectral test for randomness for dimensions 2 3 4 5 6 and
+ the product does not overflow 2 raisedTo:29.
+
+ These numbers are carefully choosen, so don't change them,
+ unless you know what you are doing!!"
+
+ modulus := 244944 " 244957 " .
+ multiplier := 1597.
+ increment := 51749.
+ seed := seedValue \\ modulus.
+
+ self step.
+
+ "Modified: / 12-11-1999 / 17:50:52 / stefan"
+ "Created: / 26-05-2007 / 21:25:10 / cg"
+!
+
+step
+ "compute the next random integer"
+
+ seed := (seed * multiplier + increment) \\ modulus
+
+ "Created: 1.4.1997 / 22:40:45 / cg"
+ "Modified: 1.4.1997 / 22:43:01 / cg"
+! !
+
+!RandomGNUSmalltalk class methodsFor:'documentation'!
+
+version
+ ^ '$Header$'
+!
+
+version_CVS
+ ^ '$Header$'
+! !
+