Squeak Class Documentation category index | class index  
 
Random
  category: Kernel-Numbers
  superclass: Object
  subclasses:

This Random Number Generator graciously contributed by David N. Smith. It is an adaptation of the Park-Miller RNG which uses Floats to avoid the need for LargeInteger arithmetic.

instance methods
  accessing
  next
next:
next:into:
nextInt:

  die rolling
  check:
check:against:
check:against:difficulty:
check:difficulty:
diceToken:
roll:

  initialization
  initialize
seed:

  private
  nextValue
seed

class methods
  examples
  example

  instance creation
  new
seed:

  testing
  bucketTest:
theItsCompletelyBrokenTest

instance methods
  accessing top  
 

next

Answer a random Float in the interval [0 to 1).


 

next:


 

next:into:


 

nextInt:

Answer a random integer in the interval [1, anInteger].


  die rolling top  
 

check:

Roll some dice, WoD-style.


 

check:against:

Roll some dice, WoD-style.


 

check:against:difficulty:

Roll some dice, WoD-style.


 

check:difficulty:

Roll some dice, WoD-style.


 

diceToken:

Private. Mini scanner, see #roll:


 

roll:

Roll some dice, DnD-style, according to this mini-grammar:
dice _ epxr {pm expr}
pm _ '+' | '-'
expr _ num | num dD | dD numP | num dD numP
dD _ 'd' | 'D'
num _ digit+
numP _ num | '%'


  initialization top  
 

initialize

Set a reasonable Park-Miller starting seed


 

seed:


  private top  
 

nextValue

This method generates random instances of Integer in the interval
0 to 16r7FFFFFFF. This method does NOT update the seed; repeated sends
answer the same value.
The algorithm is described in detail in 'Random Number Generators:
Good Ones Are Hard to Find' by Stephen K. Park and Keith W. Miller
(Comm. Asso. Comp. Mach., 31(10):1192--1201, 1988).


 

seed


class methods
  examples top  
 

example

If you just want a quick random integer, use:
10 atRandom
Every integer interval can give a random number:
(6 to: 12) atRandom
SequenceableCollections can give randomly selected elements:
'pick one of these letters randomly' atRandom
SequenceableCollections also respond to shuffled, as in:
($A to: $Z) shuffled

The correct way to use class Random is to store one in
an instance or class variable:
myGenerator _ Random new.
Then use it every time you need another number between 0.0 and 1.0 (excluding)
myGenerator next
You can also generate a positive integer
myGenerator nextInt: 10


  instance creation top  
 

new

Answer a new instance of the receiver (which is a class) with no indexable variables. Fail if the class is indexable.


 

seed:


  testing top  
 

bucketTest:

Execute this: Random bucketTest: Random new


 

theItsCompletelyBrokenTest

Random theItsCompletelyBrokenTest