Random
Number Generator
Description
This library introduces the class Random to Visual Smalltalk for use in
creating random numbers. An instance of Random is a stream whereby sending
the message #next answers a random float between 0 and 1. Instances
of Random are seeded from the system millisecond clock to assure that its
sequence begins from an undetermined origin upon creation. The library
also introduces methods to select an element from a collection at random,
shuffle the order items in a collection, or answer a subcollection of samples
from the collection. Similarly, sending message #atRandom to an
Integer answers a random integer up to and including its value. The use
of class Random as introduced by this library extends that discussed in
the "Purple" book.
Files
| Random.st |
Random number generator source |
| Randstat.st |
Scatter test application for Random |
Requirements
Visual Smalltalk/Visual Smalltalk Enterprise 3.1.2
Installation
Select File-Install from theVisual Smalltalk menu bar and install
random.st.
Examples
After installation, to confirm the generator operates correctly evaluate:
Random theItsCompletelyBrokenTest
See the comments in the source of this method for the expected
values from this test.
The sample expressions below demonstrate how methods may be used to
select random elements of a collection or interval:
#('one' 'or' 'the' 'other') atRandom.
(1 to: 10) atRandom.
'Just pick one of these letters at random' atRandom.
To answer a new collection with its elements in a random order send it
the message #shuffled:
#('one' 'or' 'the' 'other') shuffled.
(1 to: 10) shuffled.
'Just mix these letters at random' shuffled.
For a subcollection of n random samples of a collection send it
the message #sample: and provide the number of samples to take
as argument:
#('one' 'or' 'the' 'other') sample: 2.
(1 to: 10) sample: 3.
'Just select these letters at random' sample:
3.
Date monthNames sample:
2.
Finally, the enumerator method #permutationsDo: will perform
its block argument for each permutation on the order of the receiving IndexedCollection's
or Interval's elements. The method's answer is deterministic, not
random, but is included as a method helpful to some statistical operations.
#('one' 'or' 'the' 'other') permutationsDo:[:c|Transcript
show: c printString;cr].
Sending message #atRandom to an Integer answers a random integer
up to and including its value.
The file randstat.st plots a two dimensional scatter diagram of
5000 random points created using the Random class and displays their mean
and standard deviation. The performance of the random number generator
might be questioned if any pattern is evident in the plot or if the mean
deviates significantly from point 0.5@0.5. File in randstat.st
and evaluate the following to open the test window:
RandomTest new open
Additional info
The pseudo-random number algorithm used in this library is attributed to
the reference below and is described as a "minimal standard random number
generator." They claim that one should never use one worse than theirs.
Park, Stephen K. and Keith W. Miller. "Random Number Generators: Good
Ones are Hard to Find", Communications of the ACM, October 1988,
31 (10) , pp 1192-1201.
The seminal book for testing of random number generators is:
Knuth, Donald Ervin. The ART of Computer Programming, Volume II,
2nd Edition. Seminumerical Algorithms. Addison-Wesley, ISBN 0-201-03822-6,
pp 1-177.
Contact
Leandro Caniglia
caniglia@dm.uba.ar
Links to:
The
ObjectShare web site
VisualSmalltalk
Product Support
ObjectShare's
Online Documentation Site