Random.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2019 14:28:51 +0200
changeset 5050 44fa8672d102
parent 4640 84130c41071a
child 5069 c96c32f49875
permissions -rw-r--r--
#DOCUMENTATION by cg class: SharedQueue comment/format in: #next #nextWithTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4599
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
     1
"{ Encoding: utf8 }"
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
     2
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
     3
"
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
     4
 COPYRIGHT (c) 1989 by Claus Gittinger
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
     5
              All Rights Reserved
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     6
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
     7
 This software is furnished under a license and may be used
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
     8
 only in accordance with the terms of that license and with the
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    10
 be provided or otherwise made available to, or used by, any
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    11
 other person.  No title to or ownership of the software is
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    12
 hereby transferred.
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    13
"
928
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
    14
"{ Package: 'stx:libbasic2' }"
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
    15
3675
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
    16
"{ NameSpace: Smalltalk }"
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
    17
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    18
Stream subclass:#Random
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    19
	instanceVariableNames:'seed'
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    20
	classVariableNames:'RandomSalt SharedGenerator'
232
b2a2cc7ff15e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 142
diff changeset
    21
	poolDictionaries:''
4590
26321c0bc45c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4481
diff changeset
    22
	category:'Magnitude-Numbers-Random'
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    23
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    24
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
    25
!Random class methodsFor:'documentation'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    26
31
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    27
copyright
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    28
"
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    29
 COPYRIGHT (c) 1989 by Claus Gittinger
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    30
              All Rights Reserved
31
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    31
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    32
 This software is furnished under a license and may be used
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    33
 only in accordance with the terms of that license and with the
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    35
 be provided or otherwise made available to, or used by, any
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    36
 other person.  No title to or ownership of the software is
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    37
 hereby transferred.
31
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    38
"
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    39
!
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    40
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    41
documentation
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    42
"
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    43
    A facade to the actual RandomGenerator.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    44
    In previous versions, Random was a very bad fallback random generator,
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    45
    with an included warning, to not use it.
4481
7785f5a3c411 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4198
diff changeset
    46
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    47
    Now this old generator was renamed to RandomGNUSmalltalk,
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    48
    and the default generator used is the one provided by the much
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    49
    better RandomGenerator class.
4481
7785f5a3c411 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4198
diff changeset
    50
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    51
        Random new next:10   
75
claus
parents: 65
diff changeset
    52
258
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    53
    [author:]
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    54
        Claus Gittinger
947
23faa406af1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 928
diff changeset
    55
23faa406af1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 928
diff changeset
    56
    [see also:]
3394
80d468c95564 documentation link
Claus Gittinger <cg@exept.de>
parents: 3387
diff changeset
    57
        http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf
998
5f66a2dd6b7d documentation
Claus Gittinger <cg@exept.de>
parents: 978
diff changeset
    58
        RandomTT800      - a new random generator
5f66a2dd6b7d documentation
Claus Gittinger <cg@exept.de>
parents: 978
diff changeset
    59
        RandomParkMiller - another new random generator
271
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    60
"
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    61
!
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    62
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    63
examples
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    64
"
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    65
                                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    66
    |rnd|
258
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    67
271
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    68
    rnd := Random new.
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    69
    10 timesRepeat:[
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
    70
        Transcript showCR:(rnd next)
271
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    71
    ]
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    72
                                                                        [exEnd]
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    73
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    74
    rolling a dice:
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    75
                                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    76
    |rnd|
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    77
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    78
    rnd := Random new.
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    79
    10 timesRepeat:[
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
    80
        Transcript showCR:(rnd nextIntegerBetween:1 and:6)
271
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    81
    ]
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    82
                                                                        [exEnd]
31
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    83
"
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    84
! !
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    85
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
    86
!Random class methodsFor:'instance creation'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    87
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    88
new
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    89
    "return a new random generator.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    90
     Here, this instance creation message is forwarded to the standardGenerator"
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
    91
4612
5cbb0e151a6a #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
    92
    self == Random ifFalse:[^ super new].
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    93
    ^ self standard
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    94
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    95
    "
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    96
     Random new
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
    97
    "
842
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
    98
!
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
    99
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   100
random
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   101
    "return a new random generator.
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   102
     Defined here for compatibility with StreamCipher"
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   103
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   104
    ^ self standard
842
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   105
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   106
    "Created: / 12.11.1999 / 17:52:08 / stefan"
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   107
!
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   108
1878
07b7aa1394c6 setSeed API
Claus Gittinger <cg@exept.de>
parents: 1841
diff changeset
   109
seed:seedValue
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   110
    "return a new random generator with initial seed.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   111
     Here, this instance creation message is forwarded to the standardGenerator"
1878
07b7aa1394c6 setSeed API
Claus Gittinger <cg@exept.de>
parents: 1841
diff changeset
   112
4613
7229c9649f3e #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
   113
    self == Random ifFalse:[^ self basicNew seed:seedValue].
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   114
    ^ self standardGeneratorClass basicNew setSeed:seedValue
1878
07b7aa1394c6 setSeed API
Claus Gittinger <cg@exept.de>
parents: 1841
diff changeset
   115
07b7aa1394c6 setSeed API
Claus Gittinger <cg@exept.de>
parents: 1841
diff changeset
   116
    "Created: / 26-05-2007 / 21:27:18 / cg"
07b7aa1394c6 setSeed API
Claus Gittinger <cg@exept.de>
parents: 1841
diff changeset
   117
!
07b7aa1394c6 setSeed API
Claus Gittinger <cg@exept.de>
parents: 1841
diff changeset
   118
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   119
sharedGenerator
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   120
    "return a shared random generator."
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   121
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   122
    SharedGenerator isNil ifTrue:[
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   123
        SharedGenerator := self standard.
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   124
    ].
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   125
    ^ SharedGenerator
2291
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   126
!
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   127
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   128
standard
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   129
    "return the 'standard' generator"
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   130
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   131
    ^ self standardGeneratorClass new
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   132
!
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   133
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   134
standardGeneratorClass
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   135
    "return the class used for the 'standard' generator"
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   136
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   137
    ^ RandomGenerator
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   138
! !
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   139
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   140
!Random class methodsFor:'random numbers'!
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   141
3171
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   142
next
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   143
    "return the next random number in the range 0..1
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   144
     This method behaves like the corresponding instance method,
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   145
     but allows generation of random numbers without
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   146
     a need for an instance of Random to be kept around.
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   147
     This uses a common, shared generator."
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   148
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   149
    ^ self sharedGenerator next.
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   150
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   151
    "
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   152
     Transcript showCR:(Random next).
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   153
     Transcript showCR:(Random next).
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   154
     Transcript showCR:(Random next).
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   155
     Transcript showCR:(Random next).
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   156
    "
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   157
!
4cda53d2b308 class: Random
Claus Gittinger <cg@exept.de>
parents: 3134
diff changeset
   158
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   159
nextBetween:start and:stop
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   160
    "return a random number between start and stop.
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   161
     This method behaves like the corresponding instance method,
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   162
     but allows generation of random numbers without
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   163
     a need for an instance of Random to be kept around.
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   164
     This uses a common, shared generator."
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   165
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   166
    ^ self sharedGenerator nextBetween:start and:stop
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   167
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   168
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   169
     Transcript showCR:(Random nextBetween:1 and:100).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   170
     Transcript showCR:(Random nextBetween:1 and:100).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   171
     Transcript showCR:(Random nextBetween:1 and:100).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   172
     Transcript showCR:(Random nextBetween:1 and:100).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   173
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   174
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   175
    "Modified: 21.8.1997 / 18:08:56 / cg"
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   176
    "Created: 21.8.1997 / 18:09:36 / cg"
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   177
!
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   178
851
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   179
nextBoolean
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   180
    "return a boolean random.
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   181
     This method behaves like the corresponding instance method,
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   182
     but allows generation of random numbers without
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   183
     a need for an instance of Random to be kept around.
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   184
     This uses a common, shared generator."
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   185
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   186
    ^ self sharedGenerator nextBoolean.
851
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   187
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   188
    "
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   189
     Transcript showCR:(Random nextBoolean).
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   190
     Transcript showCR:(Random nextBoolean).
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   191
     Transcript showCR:(Random nextBoolean).
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   192
     Transcript showCR:(Random nextBoolean).
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   193
    "
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   194
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   195
    "Created: 21.8.1997 / 18:08:23 / cg"
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   196
!
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   197
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   198
nextInteger
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   199
    "return an integral random number.
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   200
     This method behaves like the corresponding instance method,
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   201
     but allows generation of random numbers without
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   202
     a need for an instance of Random to be kept around.
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   203
     This uses a common, shared generator."
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   204
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   205
    ^ self sharedGenerator nextInteger.
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   206
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   207
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   208
     Transcript showCR:(Random nextInteger).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   209
     Transcript showCR:(Random nextInteger).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   210
     Transcript showCR:(Random nextInteger).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   211
     Transcript showCR:(Random nextInteger).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   212
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   213
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   214
    "Created: 21.8.1997 / 18:08:23 / cg"
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   215
!
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   216
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   217
nextIntegerBetween:start and:stop
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   218
    "return an integral random number between start and stop.
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   219
     This method behaves like the corresponding instance method,
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   220
     but allows generation of random numbers without
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   221
     a need for an instance of Random to be kept around.
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   222
     This uses a common, shared generator."
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   223
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   224
    ^ self sharedGenerator nextIntegerBetween:start and:stop
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   225
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   226
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   227
     Transcript showCR:(Random nextIntegerBetween:1 and:10).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   228
     Transcript showCR:(Random nextIntegerBetween:1 and:10).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   229
     Transcript showCR:(Random nextIntegerBetween:1 and:10).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   230
     Transcript showCR:(Random nextIntegerBetween:1 and:10).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   231
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   232
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   233
    "Created: 21.8.1997 / 18:07:00 / cg"
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   234
    "Modified: 21.8.1997 / 18:08:56 / cg"
3750
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   235
!
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   236
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   237
nextLettersOrDigits:count
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   238
    "get the next count printable letters or digits [0-9A-Za-z]."
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   239
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   240
    ^ self sharedGenerator nextLettersOrDigits:count
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   241
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   242
    "
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   243
     Transcript showCR:(Random nextLettersOrDigits:10).
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   244
     Transcript showCR:(Random nextLettersOrDigits:10).
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   245
     Transcript showCR:(Random nextLettersOrDigits:10).
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   246
     Transcript showCR:(Random nextLettersOrDigits:10).
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   247
    "
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   248
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   249
    "Created: 21.8.1997 / 18:07:00 / cg"
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   250
    "Modified: 21.8.1997 / 18:08:56 / cg"
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   251
! !
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   252
3383
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   253
!Random class methodsFor:'seeding'!
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   254
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   255
randomSeed
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   256
    "return a number useful for seeding.
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   257
     This takes the current processor's time, plus the processor's process id,
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   258
     plus some value depending on the memory allocation state,
3403
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   259
     plus a random salt, and shuffles those bits around.
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   260
     The entropy returned should be reasonable enough for a good seed of a good rnd
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   261
     generator. However, keep in mind, that it only has a limited number of entropy bits
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   262
     (in the order of 32). 
3414
bfae9f81787a class: Random
Claus Gittinger <cg@exept.de>
parents: 3403
diff changeset
   263
     But it should be much better than what is commonly used in older
3403
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   264
     programs (current time) or even a constant."
3383
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   265
3414
bfae9f81787a class: Random
Claus Gittinger <cg@exept.de>
parents: 3403
diff changeset
   266
    |hash|
bfae9f81787a class: Random
Claus Gittinger <cg@exept.de>
parents: 3403
diff changeset
   267
3383
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   268
    RandomSalt isNil ifTrue:[
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   269
        RandomSalt := 1.
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   270
    ] ifFalse:[
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   271
        RandomSalt := RandomSalt + 1.
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   272
    ].
3431
4ece2027bd33 class: Random
Stefan Vogel <sv@exept.de>
parents: 3414
diff changeset
   273
4ece2027bd33 class: Random
Stefan Vogel <sv@exept.de>
parents: 3414
diff changeset
   274
    hash := MD5Stream new.
4ece2027bd33 class: Random
Stefan Vogel <sv@exept.de>
parents: 3414
diff changeset
   275
    hash 
4ece2027bd33 class: Random
Stefan Vogel <sv@exept.de>
parents: 3414
diff changeset
   276
        nextPut:RandomSalt;
4ece2027bd33 class: Random
Stefan Vogel <sv@exept.de>
parents: 3414
diff changeset
   277
        nextPut:Time microsecondClockValue; 
4ece2027bd33 class: Random
Stefan Vogel <sv@exept.de>
parents: 3414
diff changeset
   278
        nextPut:OperatingSystem getProcessId; 
4ece2027bd33 class: Random
Stefan Vogel <sv@exept.de>
parents: 3414
diff changeset
   279
        nextPut:(ObjectMemory addressOf:Object new); 
4ece2027bd33 class: Random
Stefan Vogel <sv@exept.de>
parents: 3414
diff changeset
   280
        nextPut:ObjectMemory oldSpaceUsed; 
4198
0577571b74da #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3781
diff changeset
   281
        nextPut:ObjectMemory newSpaceUsed; 
0577571b74da #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3781
diff changeset
   282
        nextPut:OperatingSystem getCPUCycleCount. 
3403
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   283
3414
bfae9f81787a class: Random
Claus Gittinger <cg@exept.de>
parents: 3403
diff changeset
   284
    "/ any other cheap sources of entropy?
3403
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   285
4198
0577571b74da #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3781
diff changeset
   286
    "/ I think there is no problem in that MD5 is not a secure hash algo here 
0577571b74da #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3781
diff changeset
   287
    "/ - the idea is to shuffle the bits around a bit
3403
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   288
    "/ (because the numbers above usually have many high bits in common)
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   289
    "/ and then condense the bits into a smaller number.
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   290
    "/ Any comment from a crypto guy here - I am willing to change this to some other hash, if that makes a problem
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   291
3414
bfae9f81787a class: Random
Claus Gittinger <cg@exept.de>
parents: 3403
diff changeset
   292
    "/ Seeding rnd generators should take some bits from the returned number (i.e. their max. seed size)
bfae9f81787a class: Random
Claus Gittinger <cg@exept.de>
parents: 3403
diff changeset
   293
    ^ LargeInteger digitBytes:hash hashValue.
3383
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   294
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   295
    "
3403
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   296
     10 timesRepeat:[Transcript showCR:self randomSeed].
3414
bfae9f81787a class: Random
Claus Gittinger <cg@exept.de>
parents: 3403
diff changeset
   297
     10 timesRepeat:[Transcript showCR:(self randomSeed bitAnd:16rFFFF)].
3403
bf8cccac67c9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   298
     self randomSeed bitAnd:16rFFFFFFFF
3383
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   299
    "
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   300
! !
836706a681b9 class: Random
Claus Gittinger <cg@exept.de>
parents: 3353
diff changeset
   301
518
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   302
!Random class methodsFor:'testing'!
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   303
793
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   304
bucketTest: randy
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   305
    "A quick-and-dirty bucket test. Prints nbuckets values on the Transcript.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   306
     Each should be 'near' the value of ntries. Any run with any value 'far' from ntries
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   307
     indicates something is very wrong. Each run generates different values.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   308
     For a slightly better test, try values of nbuckets of 200-1000 or more; 
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   309
     go get coffee.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   310
     This is a poor test; see Knuth.   
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   311
     Some 'OK' runs:
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   312
           1000 1023 998 969 997 1018 1030 1019 1054 985 1003
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   313
           1011 987 982 980 982 974 968 1044 976
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   314
           1029 1011 1025 1016 997 1019 991 954 968 999 991
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   315
           978 1035 995 988 1038 1009 988 993 976
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   316
    "
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   317
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   318
    | nbuckets buckets ntrys slot |
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   319
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   320
    nbuckets := 20.
3192
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   321
    buckets := Array new: nbuckets withAll:0.
793
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   322
    ntrys :=  1000.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   323
    ntrys*nbuckets timesRepeat: [
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   324
            slot := (randy next * nbuckets) floor + 1.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   325
            buckets at: slot put: (buckets at: slot) + 1 ].
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   326
    Transcript cr.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   327
    1 to: nbuckets do: [ :nb |
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   328
            Transcript show: (buckets at: nb) printString, ' ' ]
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   329
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   330
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   331
    "Execute this:  
3192
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   332
         self bucketTest: self new
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   333
         self bucketTest: RandomGenerator new
793
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   334
    "
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   335
!
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   336
518
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   337
chiSquareTest   
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   338
    " Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', 
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   339
            o N = number of samples
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   340
            o r  = range of random numners is [0,r)      -- condition: N >= 10r.
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   341
            o Random number generator 'passes' if chisquare value is very close to r
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   342
            o Repeat test several times, since it may be *wrong* 1 out of 10 trials."
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   343
3192
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   344
    | aGenerator frequencies n range t |
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   345
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   346
    aGenerator := self new.  "Seeded differently each time (if seeded at all)"
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   347
    range := 100.   
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   348
    n := 10000.
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   349
    frequencies := Array new:range withAll:0.
518
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   350
3192
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   351
    1 to: n do: [:i |
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   352
        t := ((aGenerator next) * range) truncated + 1.
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   353
        frequencies at:t put: ((frequencies at:t) + 1).
518
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   354
    ].
3192
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   355
    t := frequencies inject:0 into: [:nextValue :eachFreq |
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   356
            nextValue + eachFreq squared
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   357
        ].
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   358
    ^ ((range * t  / n) - n) asFloat.
518
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   359
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   360
    "
3192
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   361
     self chiSquareTest 
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   362
     RandomGenerator chiSquareTest
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   363
    "
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   364
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   365
   "
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   366
    |fail|
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   367
    fail := 0.
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   368
    10 timesRepeat:[
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   369
        |testResult|
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   370
        testResult := RandomGenerator chiSquareTest.
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   371
        (100 - testResult) abs > 20 ifTrue:[Transcript showCR:testResult. fail := fail + 1].
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   372
    ].
fcff2fefb444 class: Random
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
   373
    fail > 1 ifTrue:[self error:'test failed'].
518
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   374
    "
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   375
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   376
    "
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   377
      Sedgewick claims each chisquare number should be 100 +- 20. 
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   378
      The closer to 100, the better.
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   379
    "
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   380
! !
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   381
1201
08004b394663 method category rename
Claus Gittinger <cg@exept.de>
parents: 1175
diff changeset
   382
!Random methodsFor:'Compatibility-Squeak'!
928
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   383
2309
9f2c39c721db added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 2291
diff changeset
   384
nextInt:upperBound
9f2c39c721db added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 2291
diff changeset
   385
    "Answer a random integer in the interval [1, anInteger]."
9f2c39c721db added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 2291
diff changeset
   386
3188
e22bc4a307e1 class: Random
Claus Gittinger <cg@exept.de>
parents: 3171
diff changeset
   387
    (upperBound < 1) ifTrue:[self error:'invalid upper bound'].
e22bc4a307e1 class: Random
Claus Gittinger <cg@exept.de>
parents: 3171
diff changeset
   388
2309
9f2c39c721db added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 2291
diff changeset
   389
    ^ self nextIntegerBetween:1 and:upperBound
9f2c39c721db added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 2291
diff changeset
   390
9f2c39c721db added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 2291
diff changeset
   391
    "
9f2c39c721db added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 2291
diff changeset
   392
     Random new nextInt:10
9f2c39c721db added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 2291
diff changeset
   393
    "
9f2c39c721db added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 2291
diff changeset
   394
!
9f2c39c721db added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 2291
diff changeset
   395
928
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   396
nextIntFrom:lowerBound to:upperBound
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   397
    "return a random integer in the given range"
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   398
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   399
    ^ self nextIntegerBetween:lowerBound and:upperBound
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   400
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   401
    "
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   402
     Random new nextIntFrom:5 to:10
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   403
    "
3060
5267b3c2754c added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3026
diff changeset
   404
!
5267b3c2754c added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3026
diff changeset
   405
5267b3c2754c added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3026
diff changeset
   406
seed: anInteger
5267b3c2754c added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3026
diff changeset
   407
    self setSeed: anInteger.
5267b3c2754c added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3026
diff changeset
   408
5267b3c2754c added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3026
diff changeset
   409
    "Created: / 20-07-2013 / 01:52:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
928
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   410
! !
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   411
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   412
!Random methodsFor:'accessing-reading'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   413
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   414
next
3675
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   415
    "return the next random number in the range ]0..1["
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   416
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   417
    self subclassResponsibility.
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   418
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   419
    "
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   420
     |r|
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   421
     r := Random new.
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   422
     Transcript showCR:r next.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   423
     Transcript showCR:r next.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   424
     Transcript showCR:r next.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   425
     Transcript showCR:r next.
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   426
    "
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   427
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   428
    "Modified: 1.4.1997 / 22:44:46 / cg"
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   429
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   430
4640
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   431
next:count
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   432
    "return the next count random numbers in the range ]0..1["
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   433
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   434
    |answerStream
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   435
     cnt  "{ Class: SmallInteger }" |
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   436
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   437
    cnt := count.
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   438
    answerStream := self contentsSpecies writeStream:cnt.
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   439
    1 to:cnt do:[:index | |next|
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   440
        next := self next.
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   441
        answerStream nextPut:next.
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   442
    ].
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   443
    ^ answerStream contents
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   444
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   445
    "
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   446
     (RandomGenerator new) next:10
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   447
    "
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   448
!
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   449
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   450
next:count integerBetween:min and:max
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   451
    "return the next count random integers in min..max"
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   452
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   453
    |answerStream
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   454
     cnt  "{ Class: SmallInteger }" |
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   455
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   456
    cnt := count.
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   457
    answerStream := self contentsSpecies writeStream:cnt.
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   458
    1 to:cnt do:[:index | |next|
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   459
        next := self nextIntegerBetween:min and:max.
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   460
        answerStream nextPut:next.
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   461
    ].
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   462
    ^ answerStream contents
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   463
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   464
    "
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   465
     (RandomGenerator new) next:100 integerBetween:0 and:100
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   466
    "
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   467
!
84130c41071a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4627
diff changeset
   468
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   469
nextBetween:start and:stop
3675
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   470
    "return a random number in the range ]start..stop[.
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   471
     claus: the original GNU version has a bug in returning values
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   472
     from the interval [start .. stop+1]"
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   473
4627
62d147dd6abc #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   474
    |rndFloat rnd|
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   475
4627
62d147dd6abc #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   476
    rndFloat := self next.
62d147dd6abc #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   477
    rnd := (rndFloat * (stop - start) asFloat) + start asFloat.
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   478
    ^ rnd
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   479
4614
86242cfe8f40 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4613
diff changeset
   480
    "
86242cfe8f40 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4613
diff changeset
   481
     |r|
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   482
     r := Random new.
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   483
     Transcript showCR:(r nextBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   484
     Transcript showCR:(r nextBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   485
     Transcript showCR:(r nextBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   486
     Transcript showCR:(r nextBetween:1 and:10).
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   487
    "
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   488
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   489
    "Modified: / 21.8.1998 / 14:45:27 / cg"
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   490
!
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   491
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   492
nextBoolean
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   493
    "return true or false by random"
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   494
4614
86242cfe8f40 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4613
diff changeset
   495
    ^ self nextInteger bitAnd:16r10.
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   496
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   497
    "
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   498
    |r|
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   499
     r := Random new.
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   500
     Transcript showCR:r nextBoolean.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   501
     Transcript showCR:r nextBoolean.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   502
     Transcript showCR:r nextBoolean.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   503
     Transcript showCR:r nextBoolean.
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   504
    "
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   505
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   506
    "
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   507
     |r bag|
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   508
     r := Random new.
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   509
     bag := Bag new.
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   510
     1000000 timesRepeat:[
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   511
         bag add:(r nextBoolean).
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   512
     ].
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   513
     Transcript showCR:bag contents
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   514
    "
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   515
2045
cad1dc35c091 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2044
diff changeset
   516
    "Modified: / 22-10-2008 / 15:17:57 / cg"
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   517
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   518
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   519
nextByte
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   520
    "return the next integral random number byte in the range 0 .. 16rFF"
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   521
4614
86242cfe8f40 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4613
diff changeset
   522
    ^ self nextInteger bitAnd:16rFF
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   523
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   524
    "
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   525
     |r|
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   526
     r := self new.
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   527
     Transcript showCR:r nextByte.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   528
     Transcript showCR:r nextByte.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   529
     Transcript showCR:r nextByte.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   530
     Transcript showCR:r nextByte.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   531
    "
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   532
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   533
    "Modified: 1.4.1997 / 22:42:53 / cg"
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   534
!
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   535
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   536
nextBytes:count
978
5a4c98ef6584 comment
Claus Gittinger <cg@exept.de>
parents: 973
diff changeset
   537
    "return count random bytes (0..16rFF each)"
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   538
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   539
    ^ (1 to:count) collect:[:i | self nextByte] as:ByteArray
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   540
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   541
    "
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   542
     Transcript showCR:(RandomGenerator new nextBytes:20).  
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   543
    "
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   544
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   545
    "Modified: 1.4.1997 / 22:42:53 / cg"
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   546
!
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   547
4599
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   548
nextBytesNonZero:count
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   549
    "return count random bytes, none of which is zero (i.e. 1..16rFF each)"
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   550
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   551
    |res cnt "{Class: SmallInteger}"|
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   552
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   553
    cnt := count.
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   554
    res := ByteArray uninitializedNew:cnt.
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   555
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   556
    1 to:cnt do:[:i|
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   557
        |nextNonZero|
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   558
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   559
        [
4611
6357d7d30179 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4600
diff changeset
   560
            (nextNonZero := self nextByte) == 0
4599
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   561
        ] whileTrue.
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   562
        res at:i put:nextNonZero.
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   563
    ].
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   564
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   565
    ^ res
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   566
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   567
    "
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   568
     Transcript showCR:(Random new nextBytesNonZero:20).
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   569
    "
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   570
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   571
    "Modified: 1.4.1997 / 22:42:53 / cg"
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   572
!
169bd0f282fb #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4590
diff changeset
   573
1642
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   574
nextCharacters:count
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   575
    "get the next cnt printable characters.
3750
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   576
     We answer printable characters in the ascii range (codepoints 32 - 126)"
1642
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   577
4614
86242cfe8f40 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4613
diff changeset
   578
    ^ (1 to:count) collect:[:i | Character value:(self nextInteger \\ 94 + 32) ] as:String
1642
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   579
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   580
    "
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   581
      RandomGenerator new nextCharacters:8 
1642
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   582
    "
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   583
!
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   584
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   585
nextInteger
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   586
    "return the next integral random number,
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   587
     in the range 0 .. self maxInteger"
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   588
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   589
    self subclassResponsibility
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   590
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   591
    "
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   592
     |r|
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   593
     r := Random new.
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   594
     Transcript showCR:r nextInteger.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   595
     Transcript showCR:r nextInteger.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   596
     Transcript showCR:r nextInteger.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   597
     Transcript showCR:r nextInteger.
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   598
    "
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   599
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   600
    "Modified: 1.4.1997 / 22:42:53 / cg"
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   601
!
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   602
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   603
nextIntegerBetween:start and:stop
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   604
    "return an integral random number in [start..stop] (inclusive)"
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   605
3026
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   606
    |rnd range bytesNeeded|
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   607
3026
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   608
    range := stop - start + 1.
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   609
    "Fetch at least 2 bytes, otherwise we get some unbalanced distributions for small ranges"
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   610
    bytesNeeded := (range highBit + 15) // 8.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   611
    bytesNeeded < 4 ifTrue:[
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   612
        rnd := (((self nextByte bitShift:8) + self nextByte) bitShift:8) + self nextByte.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   613
        rnd := rnd \\ range.
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   614
    ] ifFalse:[
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   615
        rnd := (LargeInteger digitBytes:(self nextBytes:bytesNeeded)) compressed.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   616
        rnd := rnd \\ range.
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   617
    ].
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   618
    ^ rnd + start
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   619
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   620
    "shows pair-combination counts
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   621
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   622
     |r v prev this counts|
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   623
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   624
     v := TextView new .
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   625
     v extent:500@250.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   626
     v list:#('xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   627
              'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   628
              'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   629
              'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   630
              'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   631
              'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   632
              'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   633
              'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   634
              'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   635
              'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx').
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   636
     v openAndWait.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   637
     counts := (1 to:10) collect:[:row | Array new:10 withAll:0].
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   638
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   639
     r := self new.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   640
     prev := r nextIntegerBetween:1 and:10.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   641
     10000 timesRepeat:[
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   642
         100 timesRepeat:[
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   643
            this := r nextIntegerBetween:1 and:10.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   644
            (counts at:this) at:prev put:((counts at:this) at:prev)+1.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   645
         ].
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   646
         v list:(counts collect:[:row |
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   647
                    '%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d'printf:row ]).
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   648
         prev := this.
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   649
     ].
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   650
    "
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   651
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   652
    "
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   653
     |r|
3026
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   654
     r := self new.
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   655
     Transcript showCR:(r nextIntegerBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   656
     Transcript showCR:(r nextIntegerBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   657
     Transcript showCR:(r nextIntegerBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   658
     Transcript showCR:(r nextIntegerBetween:1 and:10).
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   659
    "
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   660
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   661
    "
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   662
     |r|
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   663
     r := self new.
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   664
     Transcript showCR:(r nextIntegerBetween:1 and:1000000).
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   665
     Transcript showCR:(r nextIntegerBetween:1 and:1000000).
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   666
     Transcript showCR:(r nextIntegerBetween:1 and:1000000).
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   667
     Transcript showCR:(r nextIntegerBetween:1 and:1000000).
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   668
    "
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   669
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   670
    "
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   671
     |r bag|
3026
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   672
     r := self new.
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   673
     bag := Bag new.
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   674
     1000000 timesRepeat:[
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   675
         bag add:(r nextIntegerBetween:-1 and:1).
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   676
     ].
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   677
     Transcript showCR:bag sortedCounts.
3026
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   678
    "
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   679
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   680
    "
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   681
     |r bag|
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   682
     r := self new.
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   683
     bag := Bag new.
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   684
     1000000 timesRepeat:[
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   685
         bag add:(r nextIntegerBetween:1 and:3).
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   686
     ].
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   687
     Transcript showCR:bag sortedCounts.
3026
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   688
     TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)).
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   689
    "
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   690
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   691
    "
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   692
     |r bag|
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   693
     r := self new.
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   694
     bag := Bag new.
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   695
     1000000 timesRepeat:[
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   696
         bag add:(r nextIntegerBetween:1 and:32).
3026
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   697
     ].
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   698
     Transcript showCR:bag sortedCounts.
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   699
     TestCase assert:(bag standardDeviation closeTo:(((32 squared - 1)/12) sqrt)).
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   700
    "
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   701
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   702
    "
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   703
     |r bag|
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   704
     r := self new.
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   705
     bag := Bag new.
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   706
     100000000 timesRepeat:[
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   707
         bag add:(r nextIntegerBetween:1 and:400000).
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   708
     ].
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   709
     Transcript showCR:bag sortedCounts.
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   710
     TestCase assert:(bag standardDeviation closeTo:(((400000 squared - 1)/12) sqrt)).
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   711
    "
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   712
3675
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   713
    "
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   714
     |r|
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   715
     
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   716
     r := self new.
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   717
     100000000 timesRepeat:[
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   718
         self assert:((r nextIntegerBetween:1 and:3) between:1 and:3).
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   719
     ].
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   720
    "
3750
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   721
!
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   722
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   723
nextLettersOrDigits:count
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   724
    "get a random string consisting of count printable letters or digits [0-9A-Za-z]."
3750
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   725
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   726
    |res cnt "{ Class:SmallInteger }"|
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   727
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   728
    cnt := count.
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   729
    res := String uninitializedNew:cnt.
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   730
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   731
    1 to:cnt do:[:i|
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   732
        self step.
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   733
        res 
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   734
            at:i 
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   735
            put:('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' at:(seed \\ 62 + 1)).
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   736
    ].
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   737
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   738
    ^ res
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   739
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   740
    "
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   741
      Random new nextLettersOrDigits:40 
3750
54c47e28047d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3675
diff changeset
   742
    "
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   743
! !
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   744
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   745
!Random methodsFor:'blocked methods'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   746
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   747
contents
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   748
    "blocked from use - contents makes no sense for random generators"
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   749
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   750
    self shouldNotImplement
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   751
!
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   752
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   753
nextPut: value
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   754
    "blocked from use - it makes no sense for randoms"
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   755
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   756
    self shouldNotImplement
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   757
! !
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   758
2459
efcb4bd16dc7 category of: #initialize
Claus Gittinger <cg@exept.de>
parents: 2430
diff changeset
   759
!Random methodsFor:'initialization'!
efcb4bd16dc7 category of: #initialize
Claus Gittinger <cg@exept.de>
parents: 2430
diff changeset
   760
efcb4bd16dc7 category of: #initialize
Claus Gittinger <cg@exept.de>
parents: 2430
diff changeset
   761
initialize
efcb4bd16dc7 category of: #initialize
Claus Gittinger <cg@exept.de>
parents: 2430
diff changeset
   762
    self setSeed
efcb4bd16dc7 category of: #initialize
Claus Gittinger <cg@exept.de>
parents: 2430
diff changeset
   763
! !
efcb4bd16dc7 category of: #initialize
Claus Gittinger <cg@exept.de>
parents: 2430
diff changeset
   764
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   765
!Random methodsFor:'private'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   766
2430
58d74ec1aa74 added: #addEntropy:
Stefan Vogel <sv@exept.de>
parents: 2309
diff changeset
   767
addEntropy:entropyBytes
58d74ec1aa74 added: #addEntropy:
Stefan Vogel <sv@exept.de>
parents: 2309
diff changeset
   768
    "add some entropy - ignored here"
58d74ec1aa74 added: #addEntropy:
Stefan Vogel <sv@exept.de>
parents: 2309
diff changeset
   769
58d74ec1aa74 added: #addEntropy:
Stefan Vogel <sv@exept.de>
parents: 2309
diff changeset
   770
    ^ self
58d74ec1aa74 added: #addEntropy:
Stefan Vogel <sv@exept.de>
parents: 2309
diff changeset
   771
!
58d74ec1aa74 added: #addEntropy:
Stefan Vogel <sv@exept.de>
parents: 2309
diff changeset
   772
2291
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   773
seed
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   774
    ^ seed
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   775
!
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   776
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   777
setSeed
3134
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   778
    "set the initial seed value based on the current time and processId.
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   779
     These numbers implement a maximum period generator which passes
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   780
     the spectral test for randomness for dimensions 2 3 4 5 6 and
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   781
     the product does not overflow  2 raisedTo:29.
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   782
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   783
     Use both time and processId for seed, to make different processes
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   784
     return different Random numbers"
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   785
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   786
    |newSeed|
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   787
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   788
    RandomSalt isNil ifTrue:[
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   789
        RandomSalt := 1.
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   790
    ] ifFalse:[
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   791
        RandomSalt := RandomSalt + 1.
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   792
    ].
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   793
    newSeed := RandomSalt + (Time millisecondClockValue bitXor:OperatingSystem getProcessId).
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   794
    self setSeed:newSeed.
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   795
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   796
    "Modified: / 29-05-2007 / 12:07:37 / cg"
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   797
!
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   798
5c5befefa92f class: Random
Stefan Vogel <sv@exept.de>
parents: 3060
diff changeset
   799
setSeed:seedValue
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   800
    "set the initial seed and intialize the PRNG parameters."
842
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   801
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   802
    self subclassResponsibility.
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   803
1878
07b7aa1394c6 setSeed API
Claus Gittinger <cg@exept.de>
parents: 1841
diff changeset
   804
    "Modified: / 12-11-1999 / 17:50:52 / stefan"
07b7aa1394c6 setSeed API
Claus Gittinger <cg@exept.de>
parents: 1841
diff changeset
   805
    "Created: / 26-05-2007 / 21:25:10 / cg"
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   806
!
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   807
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   808
step
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   809
    "compute the next random integer"
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   810
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   811
    self subclassResponsibility
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   812
! !
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   813
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   814
!Random methodsFor:'testing'!
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   815
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   816
atEnd
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   817
    "instances of Random can always give more numbers"
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   818
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   819
    ^ false
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   820
!
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   821
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   822
isReadable
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   823
    ^ true
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   824
!
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   825
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   826
isWritable
3781
36ed064deb4e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3776
diff changeset
   827
    "return true, if writing is supported by the receiver.
36ed064deb4e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3776
diff changeset
   828
     Random Generators never are"
36ed064deb4e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3776
diff changeset
   829
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   830
    ^ false
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   831
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   832
    "Created: 1.4.1997 / 22:38:27 / cg"
4600
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   833
!
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   834
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   835
maxInteger
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   836
    "the max value returned by self nextInteger"
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   837
ee760be38331 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4599
diff changeset
   838
    self subclassResponsibility
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   839
! !
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   840
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   841
!Random class methodsFor:'documentation'!
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   842
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   843
version
3675
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   844
    ^ '$Header$'
2291
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   845
!
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   846
0c557245d0bf changed: #new
Claus Gittinger <cg@exept.de>
parents: 2045
diff changeset
   847
version_CVS
3675
058f3f83eae0 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   848
    ^ '$Header$'
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   849
! !
3026
a02198c26f88 #nextIntegerBetween:and:
Stefan Vogel <sv@exept.de>
parents: 2459
diff changeset
   850