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