RandomBlumBlumShub.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2019 14:28:51 +0200
changeset 5050 44fa8672d102
parent 4596 1dabbb7b319d
child 5449 2d0307116c52
permissions -rw-r--r--
#DOCUMENTATION by cg class: SharedQueue comment/format in: #next #nextWithTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4325
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
     1
"
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
     2
 COPYRIGHT (c) 2014 Claus Gittinger
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
     3
              All Rights Reserved
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
     4
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
     5
 This software is furnished under a license and may be used
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
     6
 only in accordance with the terms of that license and with the
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
     8
 be provided or otherwise made available to, or used by, any
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
     9
 other person.  No title to or ownership of the software is
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    10
 hereby transferred.
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    11
"
3421
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
4325
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    14
"{ NameSpace: Smalltalk }"
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    15
3421
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
Object subclass:#RandomBlumBlumShub
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:'p q m seed'
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
4596
1dabbb7b319d #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
    20
	category:'Magnitude-Numbers-Random'
3421
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!RandomBlumBlumShub class methodsFor:'documentation'!
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
4325
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    25
copyright
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    26
"
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    27
 COPYRIGHT (c) 2014 Claus Gittinger
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    28
              All Rights Reserved
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    29
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    30
 This software is furnished under a license and may be used
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    31
 only in accordance with the terms of that license and with the
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    33
 be provided or otherwise made available to, or used by, any
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    34
 other person.  No title to or ownership of the software is
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    35
 hereby transferred.
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    36
"
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    37
!
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
    38
3421
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
documentation
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
"
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    NO WARRANTY
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    This generator uses the blum blub shub algorithm to generate random numbers.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
    It is very slow, but considered to provide good random numbers.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
    RandomBlumBlumShub new nextInteger
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
    [see also:]
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
        RandomGenerator - the default; uses the machine's /dev/random if available
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
        Random  - fast, but generates less quality random numbers
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
        RandomTT800 - another random generator
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
        RandomParkMiller - another random generator
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
        RandomMT19937 - another random generator
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
        RandomKISS - another random generator
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
        exept:libcrypt - a library containing more stuff based on hashes and cyphers
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
    [author:]
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
        Claus Gittinger.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
"
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
! !
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
!RandomBlumBlumShub class methodsFor:'instance creation'!
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
new
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
    ^ self basicNew 
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
        initialize;
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
        seed:(Random randomSeed)
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
!
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
new:seed
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
    self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
    ^ self basicNew 
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
        initialize;
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
        seed:seed
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
! !
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
!RandomBlumBlumShub methodsFor:'initialization'!
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
initialize
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
    "The two primes, p and q, should both be congruent to 3 (mod 4)"
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    p := 615389388455725613122981570401989286707.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    q := 8936277569639798554773638405675965349567.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
    m := p * q.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
    "/ (p \\ 4) = 3 ifFalse:[ self halt: 'unsuitable p' ].
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
    "/ (q \\ 4) = 3 ifFalse:[ self halt: 'unsuitable p' ].
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
    "
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
     self new initialize
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    "
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
!
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
seed:seedArg
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
    seed := seedArg.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
    self assert:(seed < m).
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
    self assert:(seed gcd:m) = 1.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
! !
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
!RandomBlumBlumShub methodsFor:'random numbers'!
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
nextBoolean
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
    "generates the next random boolean"
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
    seed := seed*seed \\ m.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
    ^ seed bitTest:1
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
!
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
nextInteger
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
    "generates the next integer in 0..FFFFFFFF"
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
    |num|
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    num := 0.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
    32 timesRepeat:[
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
        seed := (seed * seed) \\ m.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
        num := (num<<1) + (seed bitAnd:1).
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
    ].
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
    ^ num.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
    "
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
     self new nextInteger.
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
     |r|
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
     r := self new
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
     100 timesRepeat:[ Transcript showCR:r nextInteger].
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
    "
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
! !
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
!RandomBlumBlumShub class methodsFor:'documentation'!
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
version
4325
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
   134
    ^ '$Header$'
3421
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
!
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
version_CVS
4325
ccba8c047f87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3421
diff changeset
   138
    ^ '$Header$'
3421
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
! !
ffc828b5799a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140