eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'RandomGenerator':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: RandomGenerator


Inheritance:

   Object
   |
   +--Stream
      |
      +--Random
         |
         +--RandomGenerator

Package:
stx:libbasic2
Category:
Magnitude-Numbers-Random
Version:
rev: 1.26 date: 2018/03/02 16:06:22
user: cg
file: RandomGenerator.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Stefan Vogel

Description:


This is a Random number generator, 
which uses either a OS random number generator or /dev/urandom,
or an ST/X internal random number generator.

Warning: 
    this generator should not be used for cryptographic work 
UNLESS: 
    1) you are running on linux, solaris or osx,
    2) you have a working /dev/urandom.
    3) you can trust your /dev/urandom (I don't really know if any/all of them are good)

It is a facade one one of the other random generators,
and will choose (in decreasing priority) the first available generator from the following:

    - OS random system call 
    - /dev/urandom
    - rc4-based random (if present)
    - regular (untrusted) random as fallback
    


[instance variables:]

[class variables:]
    HasOSRandom     true if the operatingSystem supports random numbers
    RandFile        (if HasOSRandom is false and non-nil) the FileStream (/dev/random),
                    we get random numbers from


Related information:

    http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf
    Random
    HashRandom
    Rc4Stream

Class protocol:

adding entropy
o  addEntropy: entropyBytes

change & update
o  update: something with: aParameter from: changedObject
handle image restarts and flush any device resource handles

initialization
o  initialize
want to be informed when returning from snapshot

o  openRandFile
try to open a random device

instance creation
o  new
return a new random number generator.
Try to get system random numbers from OS (e.g. in LINUX)
or device urandom (e.g. in Linux, OSX).
If no system random numbers are available, fall back to
a cryptographic secure PRNG (like RC4Random, part of the extra libcrypt package).
As last resort fallback to the cryptographic insecure linear builtin PRNG

usage example(s):

each time, we do a new, add some entropy to the SharedRandomGenerator

queries
o  randPath
path to a file/device that is a source or random numbers

o  randomDevicePathes
paths to look for OS sources of random numbers.
Can be configured at early startup to use something special
(i.e. a special hardware random device)
If never set, the defaults /dev/random, /dev/urandom are used

o  randomDevicePathes: aCollectionOfPathesOrNil
configurable paths to look for OS sources of random numbers.
(can be set during early startup in an rc-file).
If never set, the defaults /dev/random, /dev/urandom are used


Instance protocol:

basic reading
o  nextByte
get the next random byte

usage example(s):

      RandomGenerator new nextByte

o  nextBytes: cnt
get the next cnt random bytes

usage example(s):

      RandomGenerator new nextBytes:4

reading
o  maxInteger
return the max integral random number returned by nextInteger

usage example(s):

     RandomGenerator new maxInteger.

o  next
return the next random number in the range 0..1

o  nextBoolean
return true or false by random

usage example(s):

     Distribution should approach 50/50:

     |r bag|
     r := RandomGenerator new.
     bag := Bag new.
     1000000 timesRepeat:[
         bag add:(r nextBoolean).
     ].
     Transcript showCR:bag contents

o  nextCharacters: count
get the next count printable characters.
We answer printable characters in the ascii range (codepoints 32 - 126)

usage example(s):

      RandomGenerator new nextCharacters:8

o  nextInteger
return the next integral random number,
in the range 0 .. 16r3FFFFFFF (32-bit)
or 0 .. 16r3FFFFFFFFFFFFFFF (64-bit).

o  nextLettersOrDigits: count
get the next count printable letters or digits [0-9A-Za-z].

usage example(s):

      RandomGenerator new nextLettersOrDigits:40

writing
o  nextPut: something
change the random pool by feeding in something.
Something should be some unpredictable, random event.
Ignored here

o  nextPutAll: something
change the random pool by feeding in something.
Something should be some unpredictable, random event.
Ignored here



ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Fri, 29 Mar 2024 01:43:20 GMT