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