1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
;+
; NAME:
; RANDOMNUMBERGENERATOR
;
; PURPOSE:
;
; Allows the user to obtain a sequence of pseudo-random numbers. The
; object maintains the random number generator seed in such a way that
; subsequent calls to GetRandomNumbers will guarentee that you don't
; get the same random numbers each time you ask for random numbers.
;
; AUTHOR:
;
; FANNING SOFTWARE CONSULTING
; David Fanning, Ph.D.
; 1645 Sheely Drive
; Fort Collins, CO 80526 USA
; Phone: 970-221-0438
; E-mail: david@idlcoyote.com
; Coyote's Guide to IDL Programming: http://www.idlcoyote.com
;
; CATEGORY:
; Utilities
;
; CALLING SEQUENCE:
;
; Generate three random numbers.
;
; IDL> rng = Obj_New('RandomNumberGenerator', initialSeed)
; IDL> numberOfNumbersNeeded = 3
; IDL> randomNumbers = rng -> GetRandomNumbers(numberOfNumbersNeeded)
; IDL> Print, randomNumbers
; 0.80952855 0.35878432 0.52150406
;
; Generate a sequence of 8 random digits.
; IDL> Print, rng -> GetRandomDigits(8)
; 21855786
;
; INPUT PARAMETERS FOR INIT METHOD:
;
; initialSeed: The initial seed for the random number generator. If undefined
; or absent, the number of seconds after 1 January 1970 is used.
;
; METHODS:
;
; randomNumbers -> GetRandomNumbers(d1, d2, d3, d4 d5, d6, d7, d8)
; randomDigits = obj -> GetRandomDigits(numDigets)
; obj -> SetSeed, seed
;
; MODIFICATION HISTORY:
;
; Written by David W. Fanning, 13 November 2009.
; Added GetRandomDigits method. 7 February 2010. DWF.
; Incorrect cleanup of the seed pointer fixed in the CLEANUP procedure.
; 25 February 2010, DWF.
;-
;******************************************************************************************;
; Copyright (c) 2009-2010, by Fanning Software Consulting, Inc. ;
; All rights reserved. ;
; ;
; Redistribution and use in source and binary forms, with or without ;
; modification, are permitted provided that the following conditions are met: ;
; ;
; * Redistributions of source code must retain the above copyright ;
; notice, this list of conditions and the following disclaimer. ;
; * Redistributions in binary form must reproduce the above copyright ;
; notice, this list of conditions and the following disclaimer in the ;
; documentation and/or other materials provided with the distribution. ;
; * Neither the name of Fanning Software Consulting, Inc. nor the names of its ;
; contributors may be used to endorse or promote products derived from this ;
; software without specific prior written permission. ;
; ;
; THIS SOFTWARE IS PROVIDED BY FANNING SOFTWARE CONSULTING, INC. ''AS IS'' AND ANY ;
; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ;
; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT ;
; SHALL FANNING SOFTWARE CONSULTING, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, ;
; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED ;
; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; ;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ;
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ;
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ;
;******************************************************************************************;
;------------------------------------------------------------------------------------------;
; NAME:
; RandomNumberGenerator::GetRandomDigits
;
; Purpose:
;
; Allows the user to obtain a string variable of random digits.
;
; Method Syntax:
;
; randomDigits = obj -> GetRandomDigits(numberOfDigits)
;
; Auguments:
;
; numberOfDigits: The number of digits desired. (Input, default is 8. )
;
; Keywords:
;
; BYTES: If this keyword is set, the random digits are returned
; as a byte array, rather than as a string.
;
; Return Value:
;
; randomDigits: A string variable containing the random digits.
;
;------------------------------------------------------------------------------------------;
FUNCTION RandomNumberGenerator::GetRandomDigits, digits, BYTES=bytes
COMPILE_OPT idl2
On_Error, 2
; Default is 8 digits.
IF N_Elements(digits) EQ 0 THEN digits = 8
; How many random numbers are needed to construct this number of digits?
numbersNeeded = Long( digits/16 ) + 1
; Get the numbers and use only the digits after the "0.".
randomNumbers = self -> GetRandomNumbers(numbersNeeded, /DOUBLE)
strNumbers = StrMid(String(randomNumbers, FORMAT='(F18.16)'), 2)
; Put the digits together to come up with the correct number.
digitNumbers = strNumbers[0]
FOR j=1,N_Elements(randomNumbers)-1 DO digitNumbers = digitNumbers + strNumbers[j]
; Trim the result to the correct number of digits.
IF StrLen(digitNumbers) GT digits THEN digitNumbers = StrMid(digitNumbers,0,digits)
; Do you need bytes instead of a string?
IF Keyword_Set(bytes) THEN BEGIN
numbers = BytArr(digits)
FOR j=0,digits-1 DO BEGIN
numbers[j] = Byte(Fix(StrMid(digitNumbers, j, 1)))
ENDFOR
digitNumbers = numbers
ENDIF
;Return the digits you need.
RETURN, digitNumbers
END
;------------------------------------------------------------------------------------------;
; NAME:
; RandomNumberGenerator::GetRandomNumbers
;
; Purpose:
;
; Allows the user to obtain a scalar or array of random numbers.
;
; Method Syntax:
;
; randomNumbers = obj -> GetRandomNumbers(d1, d2, d3, d4, d5, d6, d7, d8)
;
; Auguments:
;
; d1...d8 Up to 8 dimension values can be specified, as in the IDL on-line
; documentation for RANDOMU.
; Keywords:
;
; All the keywords that are defined for the IDL built-in routine RANDOMU are
; available for use here. The only difference is that non-integer numbers are
; *always* returned as double-precision values. That is to say DOUBLE is always
; set to 1. See the IDL on-line documentation for RANDOMU to understand how to use
; the following keywords:
; BINOMIAL
; DOUBLE
; GAMMA
; NORMAL
; POISSON
; UNIFORM
; LONG
;
; Return Value:
;
; randomNumbers: The scalar or array of random numbers.
;
;------------------------------------------------------------------------------------------;
FUNCTION RandomNumberGenerator::GetRandomNumbers, d1, d2, d3, d4, d5, d6, d7, d8, $
BINOMIAL=binomial, $
DOUBLE=double, $
GAMMA=gamma, $
NORMAL=normal, $
POISSON=poisson, $
UNIFORM=uniform, $
LONG=long
; I just assume any floating value is *suppose* to be in double precision.
IF N_Elements(double) EQ 0 THEN double = 1
; Yuck! Have to do this according to the number of positional parameters.
CASE N_Params() OF
0: results = RandomU(*self.seed, $
BINOMIAL=binomial, $
DOUBLE=double, $
GAMMA=gamma, $
NORMAL=normal, $
POISSON=poisson, $
UNIFORM=uniform, $
LONG=long)
1: results = RandomU(*self.seed, d1, $
BINOMIAL=binomial, $
DOUBLE=double, $
GAMMA=gamma, $
NORMAL=normal, $
POISSON=poisson, $
UNIFORM=uniform, $
LONG=long)
2: results = RandomU(*self.seed, d1, d2, $
BINOMIAL=binomial, $
DOUBLE=double, $
GAMMA=gamma, $
NORMAL=normal, $
POISSON=poisson, $
UNIFORM=uniform, $
LONG=long)
3: results = RandomU(*self.seed, d1, d2, d3, $
BINOMIAL=binomial, $
DOUBLE=double, $
GAMMA=gamma, $
NORMAL=normal, $
POISSON=poisson, $
UNIFORM=uniform, $
LONG=long)
4: results = RandomU(*self.seed, d1, d2, d3, d4, $
BINOMIAL=binomial, $
DOUBLE=double, $
GAMMA=gamma, $
NORMAL=normal, $
POISSON=poisson, $
UNIFORM=uniform, $
LONG=long)
5: results = RandomU(*self.seed, d1, d2, d3, d4, d5, $
BINOMIAL=binomial, $
DOUBLE=double, $
GAMMA=gamma, $
NORMAL=normal, $
POISSON=poisson, $
UNIFORM=uniform, $
LONG=long)
6: results = RandomU(*self.seed, d1, d2, d3, d4, d5, d6, $
BINOMIAL=binomial, $
DOUBLE=double, $
GAMMA=gamma, $
NORMAL=normal, $
POISSON=poisson, $
UNIFORM=uniform, $
LONG=long)
7: results = RandomU(*self.seed, d1, d2, d3, d4, d5, d6, d7, $
BINOMIAL=binomial, $
DOUBLE=double, $
GAMMA=gamma, $
NORMAL=normal, $
POISSON=poisson, $
UNIFORM=uniform, $
LONG=long)
8: results = RandomU(*self.seed, d1, d2, d3, d4, d5, d6, d7, d8, $
BINOMIAL=binomial, $
DOUBLE=double, $
GAMMA=gamma, $
NORMAL=normal, $
POISSON=poisson, $
UNIFORM=uniform, $
LONG=long)
ENDCASE
; Return the results.
RETURN, results
END ;----------------------------------------------------------------
PRO RandomNumberGenerator::SetSeed, theSeed
; Sets the seed for the random number generator.
IF N_Elements(theSeed) NE 0 THEN *self.seed = theSeed
END ;----------------------------------------------------------------
PRO RandomNumberGenerator::CLEANUP
Ptr_Free, self.seed
END ;----------------------------------------------------------------
FUNCTION RandomNumberGenerator::INIT, initialSeed
; Initializes the random number generator. The initialSeed is the initial
; seed for the generator. If absent, the number of seconds after 1 January 1970
; is used as the seed.
Catch, theError
IF theError NE 0 THEN BEGIN
Catch, /CANCEL
Help, /Last_Message
Return, 0
ENDIF
; Initialize the pointer.
self.seed = Ptr_New(/ALLOCATE_HEAP)
; Create the initial seed, if needed. Otherwise, just store it.
IF N_Elements(initialSeed) EQ 0 $
THEN self -> SetSeed, Systime(1) $
ELSE self -> SetSeed, initialSeed
RETURN, 1
END ;----------------------------------------------------------------
PRO RandomNumberGenerator__Define
class = { RANDOMNUMBERGENERATOR, $
seed: Ptr_New() }
END ;----------------------------------------------------------------
|