Random.st
author Stefan Vogel <sv@exept.de>
Mon, 19 Jun 2006 19:43:55 +0200
changeset 1642 0abc34691f6c
parent 1212 96dde1bb098d
child 1841 fe14c3e736ef
permissions -rw-r--r--
#nextCharacters:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
     1
"
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
     2
======================================================================
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     3
|
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     4
| Copyright (C) 1988, 1989 Free Software Foundation, Inc.
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     5
| Written by Steve Byrne.
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     6
|
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     7
| This file is part of GNU Smalltalk.
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     8
|
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     9
| GNU Smalltalk is free software; you can redistribute it and/or modify it
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    10
| under the terms of the GNU General Public License as published by the Free
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    11
| Software Foundation; either version 1, or (at your option) any later version.
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    12
| 
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    13
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    14
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    15
| FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    16
| details.
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    17
| 
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    18
| You should have received a copy of the GNU General Public License along with
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    19
| GNU Smalltalk; see the file LICENSE.  If not, write to the Free Software
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    20
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    21
|
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
    22
======================================================================
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    23
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
    24
see notice in (Random>>documentation)
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    25
"
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    26
928
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
    27
"{ Package: 'stx:libbasic2' }"
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
    28
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    29
Stream subclass:#Random
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
    30
	instanceVariableNames:'seed increment multiplier modulus'
973
ec6bced2d1f5 add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents: 947
diff changeset
    31
	classVariableNames:'RandomGenerator RandomSalt'
232
b2a2cc7ff15e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 142
diff changeset
    32
	poolDictionaries:''
b2a2cc7ff15e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 142
diff changeset
    33
	category:'Magnitude-Numbers'
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    34
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    35
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
    36
!Random class methodsFor:'documentation'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    37
31
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    38
copyright
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    39
"
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    40
======================================================================
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    41
|
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    42
| Copyright (C) 1988, 1989 Free Software Foundation, Inc.
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    43
| Written by Steve Byrne.
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    44
|
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    45
| This file is part of GNU Smalltalk.
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    46
|
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    47
| GNU Smalltalk is free software; you can redistribute it and/or modify it
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    48
| under the terms of the GNU General Public License as published by the Free
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    49
| Software Foundation; either version 1, or (at your option) any later version.
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    50
| 
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    51
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    52
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    53
| FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    54
| details.
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    55
| 
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    56
| You should have received a copy of the GNU General Public License along with
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    57
| GNU Smalltalk; see the file LICENSE.  If not, write to the Free Software
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    58
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    59
|
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    60
======================================================================
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    61
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    62
see notice in (Random>>documentation)
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
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    66
documentation
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    67
"
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    68
    random numbers - thanks to Steves GNU Smalltalk
947
23faa406af1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 928
diff changeset
    69
842
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
    70
    This implements a linear congruential maximum period random number generator
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
    71
    which passes the spectral test for randomness for dimensions 2 3 4 5 6.
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
    72
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
    73
    DO NOT USE IT FOR CRYPTOGRAPHY, because linear congruential generators
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
    74
    are predictable and can be broken easily!!
31
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    75
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    76
    Notice: although being included here,
258
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    77
            this file is NOT covered by the ST/X license, but by
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    78
            the FSF copyLeft (see copyright method).
31
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
    79
258
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    80
            You can redistribute it under the terms stated there ...
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    81
            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
    82
            this file - it has to be considered as a separate piece of
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    83
            software, which can be copied and given away without any 
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    84
            restriction from my (CG) side.
75
claus
parents: 65
diff changeset
    85
258
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    86
    [author:]
271
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    87
        Steve Byrne
258
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
    88
        Claus Gittinger
947
23faa406af1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 928
diff changeset
    89
23faa406af1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 928
diff changeset
    90
    [see also:]
998
5f66a2dd6b7d documentation
Claus Gittinger <cg@exept.de>
parents: 978
diff changeset
    91
        RandomTT800      - a new random generator
5f66a2dd6b7d documentation
Claus Gittinger <cg@exept.de>
parents: 978
diff changeset
    92
        RandomParkMiller - another new random generator
271
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    93
"
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    94
!
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    95
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    96
examples
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    97
"
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    98
                                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
    99
    |rnd|
258
c2933c08c83a documentation
Claus Gittinger <cg@exept.de>
parents: 232
diff changeset
   100
271
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   101
    rnd := Random new.
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   102
    10 timesRepeat:[
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   103
        Transcript showCR:(rnd next)
271
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   104
    ]
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   105
                                                                        [exEnd]
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   106
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   107
    rolling a dice:
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   108
                                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   109
    |rnd|
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   110
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   111
    rnd := Random new.
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   112
    10 timesRepeat:[
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   113
        Transcript showCR:(rnd nextIntegerBetween:1 and:6)
271
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   114
    ]
Claus Gittinger <cg@exept.de>
parents: 258
diff changeset
   115
                                                                        [exEnd]
31
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
   116
"
e223f3cf2995 *** empty log message ***
claus
parents: 20
diff changeset
   117
! !
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   118
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   119
!Random class methodsFor:'instance creation'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   120
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   121
new
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   122
    "return a new random generator"
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   123
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   124
    ^self basicNew setSeed
842
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   125
!
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   126
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   127
random
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   128
    "return a new random generator.
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   129
     Defined here for compatibility with StreamCipher"
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   130
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   131
    ^ self new
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   132
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   133
    "Created: / 12.11.1999 / 17:52:08 / stefan"
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   134
!
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   135
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   136
sharedGenerator
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   137
    "return a shared random generator."
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   138
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   139
    RandomGenerator isNil ifTrue:[
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   140
        RandomGenerator := self new.
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   141
    ].
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   142
    ^ RandomGenerator
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   143
! !
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   144
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   145
!Random class methodsFor:'random numbers'!
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   146
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   147
nextBetween:start and:stop
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   148
    "return a random number between start and stop.
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   149
     This method behaves like the corresponding instance method,
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   150
     but allows generation of random numbers without
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   151
     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
   152
     This uses a common, shared generator."
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   153
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   154
    ^ self sharedGenerator nextBetween:start and:stop
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   155
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   156
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   157
     Transcript showCR:(Random nextBetween:1 and:100).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   158
     Transcript showCR:(Random nextBetween:1 and:100).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   159
     Transcript showCR:(Random nextBetween:1 and:100).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   160
     Transcript showCR:(Random nextBetween:1 and:100).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   161
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   162
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   163
    "Modified: 21.8.1997 / 18:08:56 / cg"
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   164
    "Created: 21.8.1997 / 18:09:36 / cg"
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   165
!
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   166
851
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   167
nextBoolean
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   168
    "return a boolean random.
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   169
     This method behaves like the corresponding instance method,
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   170
     but allows generation of random numbers without
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   171
     a need for an instance of Random to be kept around.
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   172
     This uses a common, shared generator."
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   173
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   174
    ^ self sharedGenerator nextBoolean.
851
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   175
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   176
    "
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   177
     Transcript showCR:(Random nextBoolean).
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   178
     Transcript showCR:(Random nextBoolean).
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   179
     Transcript showCR:(Random nextBoolean).
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   180
     Transcript showCR:(Random nextBoolean).
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   181
    "
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   182
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   183
    "Created: 21.8.1997 / 18:08:23 / cg"
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   184
!
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   185
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   186
nextInteger
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   187
    "return an integral random number.
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   188
     This method behaves like the corresponding instance method,
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   189
     but allows generation of random numbers without
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   190
     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
   191
     This uses a common, shared generator."
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   192
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   193
    ^ self sharedGenerator nextInteger.
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   194
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   195
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   196
     Transcript showCR:(Random nextInteger).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   197
     Transcript showCR:(Random nextInteger).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   198
     Transcript showCR:(Random nextInteger).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   199
     Transcript showCR:(Random nextInteger).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   200
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   201
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   202
    "Created: 21.8.1997 / 18:08:23 / cg"
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   203
!
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   204
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   205
nextIntegerBetween:start and:stop
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   206
    "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
   207
     This method behaves like the corresponding instance method,
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   208
     but allows generation of random numbers without
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   209
     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
   210
     This uses a common, shared generator."
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   211
1128
72d61e6ef2b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1127
diff changeset
   212
    ^ self sharedGenerator nextIntegerBetween:start and:stop
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   213
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   214
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   215
     Transcript showCR:(Random nextIntegerBetween:1 and:10).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   216
     Transcript showCR:(Random nextIntegerBetween:1 and:10).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   217
     Transcript showCR:(Random nextIntegerBetween:1 and:10).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   218
     Transcript showCR:(Random nextIntegerBetween:1 and:10).
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   219
    "
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   220
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   221
    "Created: 21.8.1997 / 18:07:00 / cg"
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   222
    "Modified: 21.8.1997 / 18:08:56 / cg"
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   223
! !
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   224
518
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   225
!Random class methodsFor:'testing'!
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   226
793
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   227
bucketTest: randy
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   228
    "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
   229
     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
   230
     indicates something is very wrong. Each run generates different values.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   231
     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
   232
     go get coffee.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   233
     This is a poor test; see Knuth.   
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   234
     Some 'OK' runs:
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   235
           1000 1023 998 969 997 1018 1030 1019 1054 985 1003
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   236
           1011 987 982 980 982 974 968 1044 976
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   237
           1029 1011 1025 1016 997 1019 991 954 968 999 991
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   238
           978 1035 995 988 1038 1009 988 993 976
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   239
    "
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   240
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   241
    | nbuckets buckets ntrys slot |
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   242
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   243
    nbuckets := 20.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   244
    buckets := Array new: nbuckets.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   245
    buckets atAllPut: 0.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   246
    ntrys :=  1000.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   247
    ntrys*nbuckets timesRepeat: [
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   248
            slot := (randy next * nbuckets) floor + 1.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   249
            buckets at: slot put: (buckets at: slot) + 1 ].
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   250
    Transcript cr.
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   251
    1 to: nbuckets do: [ :nb |
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   252
            Transcript show: (buckets at: nb) printString, ' ' ]
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   253
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   254
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   255
    "Execute this:  
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   256
     Random bucketTest: Random new
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   257
    "
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   258
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   259
!
037f3ceb4e0a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 680
diff changeset
   260
518
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   261
chiSquareTest   
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   262
    " Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', 
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   263
            o N = number of samples
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   264
            o r  = range of random numners is [0,r)      -- condition: N >= 10r.
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   265
            o Random number generator 'passes' if chisquare value is very close to r
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   266
            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
   267
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   268
    | aRand frequencies n range t chisquare |
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   269
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   270
    chisquare := Array new: 10.     "Collect results from 10 trails"
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   271
    1 to: 10 do: [:k |       "k = trail number"
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   272
        aRand := Random new.  "Seeded differently each time"
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   273
        range := 100.   
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   274
        n := 1000.
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   275
        frequencies := Array new: range.
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   276
        1 to: frequencies size do: [ :i | frequencies at: i put: 0 ].
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   277
        1 to: n do: [ :i |
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   278
                t := ((aRand next) * range) truncated.
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   279
                frequencies at: (t+1) put: ((frequencies at: (t + 1)) + 1) ].
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   280
        t := 0.
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   281
        1 to: range do: [ :i |
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   282
                t := t +  ((frequencies at: i) squared) ].
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   283
        chisquare at: k put: (((range * t  / n) - n) asFloat).
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   284
    ].
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   285
    ^ chisquare
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   286
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   287
    "
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   288
     Random chiSquareTest 
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   289
    "
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   290
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   291
    "
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   292
      Sedgewick claims each chisquare number should be 100 +- 20. 
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   293
      The closer to 100, the better.
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   294
    "
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   295
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   296
    "Modified: 16.4.1997 / 16:48:26 / cg"
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   297
! !
f5fe8b630e4c chiSquareTest
Claus Gittinger <cg@exept.de>
parents: 517
diff changeset
   298
1201
08004b394663 method category rename
Claus Gittinger <cg@exept.de>
parents: 1175
diff changeset
   299
!Random methodsFor:'Compatibility-Squeak'!
928
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   300
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   301
nextIntFrom:lowerBound to:upperBound
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   302
    "return a random integer in the given range"
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   303
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   304
    ^ self nextIntegerBetween:lowerBound and:upperBound
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   305
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   306
    "
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   307
     Random new nextIntFrom:5 to:10
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   308
    "
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   309
! !
86c222baa183 squeak compatibility
Claus Gittinger <cg@exept.de>
parents: 851
diff changeset
   310
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   311
!Random methodsFor:'accessing-reading'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   312
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   313
next
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   314
    "return the next random number in the range 0..1"
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   315
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   316
    ^ self nextInteger / modulus asFloat
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   317
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   318
    "|r|
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   319
     r := Random new.
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   320
     Transcript showCR:r next.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   321
     Transcript showCR:r next.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   322
     Transcript showCR:r next.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   323
     Transcript showCR:r next.
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   324
    "
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   325
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   326
    "Modified: 1.4.1997 / 22:44:46 / cg"
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   327
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   328
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   329
nextBetween:start and:stop
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   330
    "return a random number between start and stop.
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   331
     claus: the original GNU version has a bug in returning values
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   332
     from the interval [start .. stop+1]"
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   333
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   334
    |rnd|
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   335
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   336
    rnd := self next.
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   337
    rnd := rnd * (stop - start) asFloat.
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   338
    rnd := rnd + start asFloat.
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   339
    ^ rnd
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   340
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   341
    "|r|
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   342
     r := Random new.
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   343
     Transcript showCR:(r nextBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   344
     Transcript showCR:(r nextBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   345
     Transcript showCR:(r nextBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   346
     Transcript showCR:(r nextBetween:1 and:10).
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   347
    "
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   348
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   349
    "Modified: / 21.8.1998 / 14:45:27 / cg"
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   350
!
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   351
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   352
nextBoolean
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   353
    "return true or false by random"
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   354
851
e1c3af12daca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 842
diff changeset
   355
    ^ self next < 0.5
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   356
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   357
    "|r|
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   358
     r := Random new.
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   359
     Transcript showCR:r nextBoolean.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   360
     Transcript showCR:r nextBoolean.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   361
     Transcript showCR:r nextBoolean.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   362
     Transcript showCR:r nextBoolean.
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   363
    "
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   364
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   365
    "
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   366
     |r bag|
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   367
     r := Random new.
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   368
     bag := Bag new.
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   369
     1000000 timesRepeat:[
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   370
         bag add:(r nextBoolean).
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   371
     ].
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   372
     Transcript showCR:bag contents
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   373
    "
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   374
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   375
    "Modified: / 21.8.1998 / 14:45:55 / cg"
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   376
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   377
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   378
nextByte
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   379
    "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
   380
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   381
    self step.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   382
    ^ seed bitAnd:16rFF
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   383
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   384
    "
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   385
     |r|
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   386
     r := Random new.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   387
     Transcript showCR:r nextByte.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   388
     Transcript showCR:r nextByte.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   389
     Transcript showCR:r nextByte.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   390
     Transcript showCR:r nextByte.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   391
    "
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   392
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   393
    "Modified: 1.4.1997 / 22:42:53 / cg"
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   394
!
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   395
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   396
nextBytes:count
978
5a4c98ef6584 comment
Claus Gittinger <cg@exept.de>
parents: 973
diff changeset
   397
    "return count random bytes (0..16rFF each)"
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   398
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   399
    |res|
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   400
1175
6c846afd9fbd *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1128
diff changeset
   401
    res := ByteArray uninitializedNew:count.
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   402
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   403
    1 to:count do:[:i|
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   404
        self step.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   405
        res at:i put:(seed bitAnd:16rFF).
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   406
    ].
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   407
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   408
    ^ res
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   409
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   410
    "
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   411
     |r|
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   412
     r := Random new.
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   413
     Transcript showCR:(r nextBytes:20).
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   414
    "
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   415
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   416
    "Modified: 1.4.1997 / 22:42:53 / cg"
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   417
!
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   418
1642
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   419
nextCharacters:count
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   420
    "get the next cnt printable characters.
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   421
     We answer characters in the ascii range (codepoints 32 - 127)"
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   422
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   423
    |res|
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   424
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   425
    res := String uninitializedNew:count.
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   426
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   427
    1 to:count do:[:i|
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   428
        self step.
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   429
        res at:i put:(Character value:(seed \\ 95 + 32)).
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   430
    ].
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   431
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   432
    ^ res
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   433
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   434
    "
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   435
      Random new nextCharacters:8
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   436
    "
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   437
!
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   438
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   439
nextInteger
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   440
    "return the next integral random number,
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   441
     in the range 0 .. 16r3FFFFFFF.
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   442
     From Sedgewick's 'Algorithms', based on Lehmer's method"
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   443
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   444
    self step.
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   445
    ^ seed
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   446
840
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   447
    "
5f6559515588 Add #nextByte and #nextBytes:
Stefan Vogel <sv@exept.de>
parents: 793
diff changeset
   448
     |r|
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   449
     r := Random new.
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   450
     Transcript showCR:r nextInteger.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   451
     Transcript showCR:r nextInteger.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   452
     Transcript showCR:r nextInteger.
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   453
     Transcript showCR:r nextInteger.
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   454
    "
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   455
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   456
    "Modified: 1.4.1997 / 22:42:53 / cg"
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   457
!
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   458
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   459
nextIntegerBetween:start and:stop
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   460
    "return an integral random number between start and stop"
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   461
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   462
    |rnd|
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   463
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   464
    rnd := self next.                               
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   465
    rnd := rnd * ((stop - start) asFloat + 1.0).    
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   466
    ^ rnd truncated + start
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   467
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   468
    "
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   469
     |r|
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   470
     r := Random new.
350
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   471
     Transcript showCR:(r nextIntegerBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   472
     Transcript showCR:(r nextIntegerBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   473
     Transcript showCR:(r nextIntegerBetween:1 and:10).
93d5932c76e6 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 271
diff changeset
   474
     Transcript showCR:(r nextIntegerBetween:1 and:10).
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   475
    "
561
f00675b5531f added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents: 518
diff changeset
   476
680
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   477
    "
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   478
     |r bag|
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   479
     r := Random new.
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   480
     bag := Bag new.
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   481
     1000000 timesRepeat:[
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   482
         bag add:(r nextIntegerBetween:-1 and:1).
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   483
     ].
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   484
     Transcript showCR:bag contents
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   485
    "
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   486
a7b98fdac3dc fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   487
    "Modified: / 21.8.1998 / 14:45:04 / cg"
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   488
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   489
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   490
nextMatchFor: aNumber
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   491
    "generate the next random, return true iff it has the same
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   492
     value as aNumber. Redefined to avoid endless reading."
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   493
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   494
    ^self next = aNumber
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   495
! !
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   496
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   497
!Random methodsFor:'blocked methods'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   498
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   499
contents
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   500
    "blocked from use - contents makes no sense for random generators"
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   501
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   502
    self shouldNotImplement
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   503
!
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   504
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   505
nextPut: value
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   506
    "blocked from use - it makes no sense for randoms"
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   507
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   508
    self shouldNotImplement
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   509
! !
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   510
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   511
!Random methodsFor:'private'!
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   512
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   513
setSeed
842
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   514
    "set the initial seed value based on the current time.
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   515
     These numbers implement a maximum period generator which passes
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   516
     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
   517
     the product does not overflow  2 raisedTo:29.
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   518
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   519
     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
   520
     unless you know what you are doing!!"
17
f3e4377c8e1f *** empty log message ***
claus
parents: 2
diff changeset
   521
1212
96dde1bb098d Put processId in random seed
Stefan Vogel <sv@exept.de>
parents: 1201
diff changeset
   522
    "Use both time and processId for seed, to make different processes
96dde1bb098d Put processId in random seed
Stefan Vogel <sv@exept.de>
parents: 1201
diff changeset
   523
     return different Random numbers"
96dde1bb098d Put processId in random seed
Stefan Vogel <sv@exept.de>
parents: 1201
diff changeset
   524
    seed := (Time millisecondClockValue 
96dde1bb098d Put processId in random seed
Stefan Vogel <sv@exept.de>
parents: 1201
diff changeset
   525
             bitXor:OperatingSystem getProcessId) bitAnd:16rFFFF.
973
ec6bced2d1f5 add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents: 947
diff changeset
   526
    RandomSalt isNil ifTrue:[
ec6bced2d1f5 add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents: 947
diff changeset
   527
        RandomSalt := 1.
1127
2bace63d7bd5 changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   528
    ] ifFalse:[
2bace63d7bd5 changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   529
        RandomSalt := RandomSalt + 1.
973
ec6bced2d1f5 add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents: 947
diff changeset
   530
    ].
ec6bced2d1f5 add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents: 947
diff changeset
   531
    seed := seed + RandomSalt.
842
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   532
    multiplier := 1597.
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   533
    increment := 51749.
1127
2bace63d7bd5 changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   534
    modulus := 244944 " 244957 " .
2bace63d7bd5 changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   535
2bace63d7bd5 changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   536
    self step.
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   537
842
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   538
    "Modified: / 1.4.1997 / 22:44:13 / cg"
718e00244ba4 SOme doku and defined #random.
Stefan Vogel <sv@exept.de>
parents: 840
diff changeset
   539
    "Modified: / 12.11.1999 / 17:50:52 / stefan"
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   540
!
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   541
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   542
step
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   543
    "compute the next random integer"
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   544
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   545
    "/ seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF.
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   546
    seed := seed * multiplier + increment \\ modulus
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   547
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   548
    "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
   549
    "Modified: 1.4.1997 / 22:43:01 / cg"
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
   550
! !
65
8fdf5f30225f *** empty log message ***
claus
parents: 36
diff changeset
   551
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   552
!Random methodsFor:'testing'!
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   553
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   554
atEnd
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   555
    "instances of Random can always give more numbers"
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   556
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   557
    ^ false
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   558
!
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   559
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   560
isReadable
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   561
    ^ true
517
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   562
!
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   563
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   564
isWritable
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   565
    ^ false
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   566
4e47b198c9bc allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents: 350
diff changeset
   567
    "Created: 1.4.1997 / 22:38:27 / cg"
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   568
! !
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   569
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   570
!Random class methodsFor:'documentation'!
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   571
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   572
version
1642
0abc34691f6c #nextCharacters:
Stefan Vogel <sv@exept.de>
parents: 1212
diff changeset
   573
    ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.36 2006-06-19 17:43:55 stefan Exp $'
142
7f9b4315058c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   574
! !