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.31 date: 2023/05/24 15:41:59
user: cg
file: RandomGenerator.st directory: libbasic2
module: stx stc-classLibrary: libbasic2

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

copyright

COPYRIGHT (c) 2007 by eXept Software AG All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the inclusion of the above copyright notice. This software may not be provided or otherwise made available to, or used by, any other person. No title to or ownership of the software is hereby transferred.

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

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

Usage example(s):

     RandomGenerator new maxInteger.

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

Usage example(s):

     |r|
     r := RandomGenerator new.
     Transcript showCR:r next.
     Transcript showCR:r next.
     Transcript showCR:r next.
     Transcript showCR:r next.

o  nextBoolean
return true or false by random

Usage example(s):

     |r|
     r := RandomGenerator new.
     Transcript showCR:r nextBoolean.
     Transcript showCR:r nextBoolean.
     Transcript showCR:r nextBoolean.
     Transcript showCR:r nextBoolean.

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).

Usage example(s):

     |r|
     r := RandomGenerator new.
     Transcript showCR:r nextInteger.
     Transcript showCR:r nextInteger.
     Transcript showCR:r nextInteger.
     Transcript showCR:r nextInteger.

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.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Thu, 21 Nov 2024 12:20:39 GMT