|
Class: RandomGNUSmalltalk
Object
|
+--Stream
|
+--Random
|
+--RandomGNUSmalltalk
- Package:
- stx:libbasic2
- Category:
- Magnitude-Numbers-Random
- Version:
- rev:
1.4
date: 2021/01/20 12:15:50
- user: cg
- file: RandomGNUSmalltalk.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
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.
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)
accessing-reading
-
maxInteger
-
-
nextBoolean
-
return true or false by random
Usage example(s):
|r|
r := Random new.
Transcript showCR:r nextBoolean.
Transcript showCR:r nextBoolean.
Transcript showCR:r nextBoolean.
Transcript showCR:r nextBoolean.
|
Usage example(s):
|r bag|
r := Random new.
bag := Bag new.
1000000 timesRepeat:[
bag add:(r nextBoolean).
].
Transcript showCR:bag contents
|
-
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!
Usage example(s):
|r|
r := Random new.
Transcript showCR:r nextInteger.
Transcript showCR:r nextInteger.
Transcript showCR:r nextInteger.
Transcript showCR:r nextInteger.
|
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!
-
step
-
compute the next random integer
reading
-
next
-
return the next random number in the range ]0..1[
Usage example(s):
|r|
r := Random new.
Transcript showCR:r next.
Transcript showCR:r next.
Transcript showCR:r next.
Transcript showCR:r next.
|
|rnd|
rnd := RandomGNUSmalltalk new.
10 timesRepeat:[
Transcript showCR:(rnd next)
]
|
rolling a dice:
|rnd|
rnd := RandomGNUSmalltalk new.
10 timesRepeat:[
Transcript showCR:(rnd nextIntegerBetween:1 and:6)
]
|
|