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