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