├── .gitattributes ├── .github └── workflows │ └── test.yml ├── LICENSE ├── README.mess ├── adler32.lisp ├── cityhash.lisp ├── docs └── index.html ├── documentation.lisp ├── generator.lisp ├── hammersley.lisp ├── histogram.lisp ├── implementation.lisp ├── kiss.lisp ├── linear-congruence.lisp ├── mersenne-twister.lisp ├── middle-square.lisp ├── murmurhash.lisp ├── package.lisp ├── pcg.lisp ├── primes.lisp ├── protocol.lisp ├── quasi.lisp ├── random-state.asd ├── rc4.lisp ├── sobol.lisp ├── squirrel.lisp ├── staple.ext.lisp ├── test.lisp ├── toolkit.lisp ├── tt800.lisp ├── viewer.lisp ├── xkcd.lisp └── xorshift.lisp /.gitattributes: -------------------------------------------------------------------------------- 1 | 2 | doc/ linguist-vendored 3 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: 5 | pull_request: 6 | branches: [ "master" ] 7 | workflow_dispatch: 8 | 9 | jobs: 10 | test: 11 | name: ${{ matrix.lisp }} on ${{ matrix.os }} 12 | strategy: 13 | matrix: 14 | # clisp doesn't run on ubuntu VMs currently 15 | lisp: [sbcl-bin,ccl-bin/1.12.2,ecl,allegro/10.1express,cmu-bin/21e,abcl/1.9.2] 16 | os: [ubuntu-latest] 17 | fail-fast: false 18 | runs-on: ${{ matrix.os }} 19 | 20 | steps: 21 | - name: prepare 22 | uses: 40ants/setup-lisp@v2 23 | with: 24 | roswell-version: v22.12.14.113 25 | asdf-version: 3.3.6 26 | - uses: actions/checkout@v3 27 | 28 | - name: cache .roswell 29 | id: cache-dot-roswell 30 | uses: actions/cache@v3 31 | with: 32 | path: ~/.roswell 33 | key: ${{ runner.os }}-dot-roswell-${{ matrix.lisp }}-${{ hashFiles('**/*.asd') }} 34 | restore-keys: | 35 | ${{ runner.os }}-dot-roswell-${{ matrix.lisp }}- 36 | ${{ runner.os }}-dot-roswell- 37 | 38 | - name: install parachute 39 | run: | 40 | ros install parachute 41 | echo "$HOME/.roswell/bin" >> $GITHUB_PATH 42 | 43 | - name: update ql dist if we have one cached 44 | shell: bash 45 | run: ros -e "(ql:update-all-dists :prompt nil)" 46 | 47 | - name: load code and run tests 48 | shell: bash 49 | run: | 50 | run-parachute -l "random-state-test" "org.shirakumo.random-state.test" 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Yukari Hafner 2 | 3 | This software is provided 'as-is', without any express or implied 4 | warranty. In no event will the authors be held liable for any damages 5 | arising from the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any purpose, 8 | including commercial applications, and to alter it and redistribute it 9 | freely, subject to the following restrictions: 10 | 11 | 1. The origin of this software must not be misrepresented; you must not 12 | claim that you wrote the original software. If you use this software 13 | in a product, an acknowledgment in the product documentation would be 14 | appreciated but is not required. 15 | 2. Altered source versions must be plainly marked as such, and must not be 16 | misrepresented as being the original software. 17 | 3. This notice may not be removed or altered from any source distribution. 18 | -------------------------------------------------------------------------------- /README.mess: -------------------------------------------------------------------------------- 1 | ## About random-state 2 | This is a collection of pseudo random number generators (PRNGs) and quasi-random number generators (QRNGs). While Common Lisp does provide a ``RANDOM`` function, it does not allow the user to pass an explicit seed, nor to portably exchange the random state between implementations. This can be a headache in cases like games, where a controlled seeding process is very useful. 3 | 4 | ## How To 5 | For both curiosity and convenience, this library offers multiple algorithms to generate random numbers, as well as a bunch of generally useful methods to produce desired ranges. 6 | 7 | :: common lisp 8 | (loop with generator = (random-state:make-generator :mersenne-twister-32 123) 9 | repeat 10 10 | collect (random-state:random-int generator 0 10)) 11 | ; => (8 10 9 5 3 10 9 2 9 2) 12 | :: 13 | 14 | Several methods to compute random numbers in certain ranges are provided in advance: ``random-byte``, ``random-bytes``, ``random-sequence``, ``random-unit``, ``random-float``, and ``random-int``. Each of those can also be used with just the name of the generator you'd like to use as the first argument, in which case a global instance will be used. 15 | 16 | For generators that are hash-based, such as ``squirrel``, additional noise functions are provided: ``random-1d``, ``random-2d``, and ``random-3d``, and some functions to manipulate the stepping of the generator: ``index``, and ``rewind``. 17 | 18 | ## Implementations 19 | The following algorithms are currently implemented: 20 | 21 | - Adler 22 | - Cityhash 23 | - Generic QRNG 24 | - Hammersley 25 | - KISS 26 | - Linear Congruence 27 | - Mersenne Twister 28 | - Middle Square 29 | - Murmurhash 30 | - PCG 31 | - RC4 32 | - Sobol 33 | - Squirrelhash 34 | - TT800 35 | - XKCD 36 | - Xorshift / Yorshift RNGs 37 | 38 | The protocol also implements a fallback to the implementation's own ``random-state``. 39 | -------------------------------------------------------------------------------- /adler32.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator adler32 32 (hash-generator) () 4 | (:copy 5 | (make-adler32 :%seed (adler32-%seed generator) 6 | :index (adler32-index generator))) 7 | (:hash 8 | (declare (optimize speed (safety 1))) 9 | (let ((s1 1) 10 | (s2 0) 11 | (bits (fit-bits 32 (+ seed index)))) 12 | (macrolet ((it (byte) 13 | `(setf s1 (fit-bits 32 (mod (+ s1 ,byte) 65521)) 14 | s2 (fit-bits 32 (mod (+ s2 s1) 65521))))) 15 | (it (ldb (byte 8 0) bits)) 16 | (it (ldb (byte 8 8) bits)) 17 | (it (ldb (byte 8 16) bits)) 18 | (it (ldb (byte 8 24) bits))) 19 | (logior (ash s2 16) s1)))) 20 | -------------------------------------------------------------------------------- /cityhash.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator cityhash-64 64 (hash-generator) () 4 | (:copy 5 | (make-cityhash-64 :%seed (cityhash-64-%seed generator) 6 | :index (cityhash-64-index generator))) 7 | (:hash 8 | (declare (optimize speed (safety 1))) 9 | (let ((k1 #xb492b66fbe98f273) 10 | (k2 #x9ae16a3b2f90404f)) 11 | (flet ((hashlen16 (low high &optional (kmul #x9ddfea08eb382d69)) 12 | (declare (type (unsigned-byte 64) low high kmul)) 13 | (let ((a (fit-bits 64 (* kmul (logxor low high))))) 14 | (update 64 a logxor (ash a -47)) 15 | (let ((b (fit-bits 64 (* kmul (logxor high a))))) 16 | (update 64 b logxor (ash b -47)) 17 | (update 64 b * kmul)))) 18 | (rotate (val shift) 19 | (declare (type (unsigned-byte 64) val)) 20 | (declare (type (unsigned-byte 32) shift)) 21 | (logior (ash val (- shift)) 22 | (ash val (- 64 shift))))) 23 | (declare (inline hashlen16 rotate)) 24 | (let* ((mul (fit-bits 64 (+ k2 (* 8 2)))) 25 | (a (fit-bits 64 (+ index k2))) 26 | (b (fit-bits 64 index)) 27 | (c (fit-bits 64 (+ a (* mul (rotate b 37))))) 28 | (d (fit-bits 64 (* mul (+ b (rotate a 25))))) 29 | (e (fit-bits 64 (+ (hashlen16 c d mul) (* (logxor b (ash b -47)) k1) c)))) 30 | (hashlen16 e seed)))))) 31 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | Random State

random state

1.0.1

Portable random number generation.

About random-state

This is a collection of pseudo random number generators (PRNGs) and quasi-random number generators (QRNGs). While Common Lisp does provide a RANDOM function, it does not allow the user to pass an explicit seed, nor to portably exchange the random state between implementations. This can be a headache in cases like games, where a controlled seeding process is very useful.

How To

For both curiosity and convenience, this library offers multiple algorithms to generate random numbers, as well as a bunch of generally useful methods to produce desired ranges.

(loop with generator = (random-state:make-generator :mersenne-twister-32 123)
  2 |       repeat 10
  3 |       collect (random-state:random-int generator 0 10))
  4 | ; => (8 10 9 5 3 10 9 2 9 2)

Several methods to compute random numbers in certain ranges are provided in advance: random-byte, random-bytes, random-sequence, random-unit, random-float, and random-int. Each of those can also be used with just the name of the generator you'd like to use as the first argument, in which case a global instance will be used.

For generators that are hash-based, such as squirrel, additional noise functions are provided: random-1d, random-2d, and random-3d, and some functions to manipulate the stepping of the generator: index, and rewind.

Implementations

The following algorithms are currently implemented:

  • Adler

  • Cityhash

  • Generic QRNG

  • Hammersley

  • KISS

  • Linear Congruence

  • Mersenne Twister

  • Middle Square

  • Murmurhash

  • PCG

  • RC4

  • Sobol

  • Squirrelhash

  • TT800

  • XKCD

  • Xorshift / Yorshift RNGs

The protocol also implements a fallback to the implementation's own random-state.

System Information

1.0.1
Yukari Hafner
zlib

Definition Index

-------------------------------------------------------------------------------- /documentation.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | ;; generator.lisp 4 | (docs:define-docs 5 | (function global-generator 6 | "Returns a global instance of a generator. 7 | 8 | You may also SETF this place to name specific generators of your own. 9 | 10 | See MAKE-GENERATOR") 11 | 12 | (function ensure-generator 13 | "Ensures the argument is an object usable for random number generation. 14 | 15 | See GLOBAL-GENERATOR 16 | See GENERATOR") 17 | 18 | (function list-generator-types 19 | "Lists the types of generators supported by the library. 20 | 21 | You may use any of these types to call MAKE-GENERATOR with. 22 | 23 | See MAKE-GENERATOR") 24 | 25 | (type generator 26 | "General class for any random number generator. 27 | 28 | See LIST-GENERATOR-TYPES 29 | See SEED 30 | See RESEED 31 | See NEXT-BYTE 32 | See BITS-PER-BYTE 33 | See COPY 34 | See MULTIVARIATE-P 35 | See MAKE-GENERATOR 36 | See STATEFUL-GENERATOR 37 | See HASH-GENERATOR") 38 | 39 | (function seed 40 | "Returns the seed that was used to initialise the generator. 41 | 42 | See GENERATOR") 43 | 44 | (function reseed 45 | "Reset the RNG and seed it with the given seed number. 46 | 47 | If T is passed for the new seed, a random seed as determined by 48 | HOPEFULLY-SUFFICIENTLY-RANDOM-SEED is used. 49 | 50 | See HOPEFULLY-SUFFICIENTLY-RANDOM-SEED 51 | See GENERATOR") 52 | 53 | (function next-byte 54 | "Returns the next byte (not octet) of random state. 55 | 56 | The value returned is in accordance to the spec of BITS-PER-BYTE. 57 | If the spec is an integer, the returned integer must be in the range 58 | of 59 | 60 | [ 0, 1 << BITS-PER-BYTE GENERATOR [ 61 | 62 | If the spec is SINGLE-FLOAT or DOUBLE-FLOAT, the returned float must 63 | be in the range of 64 | 65 | [ 0, 1 [ 66 | 67 | If the spec is a list, the returned value is an array with each of its 68 | elements according to the above description. 69 | 70 | See RANDOM-INT 71 | See RANDOM-BYTES 72 | See GENERATOR") 73 | 74 | (function bits-per-byte 75 | "Returns the number of bits of randomness returned by the generator for each NEXT-BYTE call. 76 | 77 | This may either be an integer, describing the bits of randomness returned, 78 | the symbol SINGLE-FLOAT or DOUBLE-FLOAT in which case NEXT-BYTE returns a unit float, 79 | or a list composed of the aforementioned, in which case NEXT-BYTE returns an array 80 | of such bytes. 81 | 82 | See NEXT-BYTE 83 | See GENERATOR") 84 | 85 | (function multivariate-p 86 | "Returns true if the generator is multivariate and returns an array of bytes on NEXT-BYTE. 87 | 88 | See NEXT-BYTE 89 | See GENERATOR") 90 | 91 | (function copy 92 | "Creates a fresh copy of the given generator. 93 | 94 | This copy will return an identical sequence of bytes as the 95 | original. Meaning, the following invariant holds true: 96 | 97 | (loop with copy = (copy generator) always (= (next-byte generator) (next-byte copy))) 98 | 99 | See GENERATOR") 100 | 101 | (function make-generator 102 | "Creates a new generator of the given type. 103 | 104 | You may pass an optional seed to initialise the generator with. If no 105 | seed is specified, each constructed generator of the same type will 106 | return the same sequence of numbers. 107 | 108 | See RESEED 109 | See GENERATOR") 110 | 111 | (function define-generator 112 | "Define a new random number generator type. 113 | 114 | BITS-PER-BYTE is a form that should evaluate to the byte spec for the 115 | generator. 116 | 117 | SUPER should be a list of the following structure: 118 | 119 | SUPER ::= (type SLOT*) 120 | SLOT ::= (slot-name initform) 121 | type --- The structure-type name to use as supertype 122 | slot-name --- The name of a slot in the supertype 123 | initform --- The initial value for the slot 124 | 125 | SLOTS should be a list of additional structure slot specs to hold the 126 | generator's state. 127 | 128 | BODIES should be any number of body expressions, each of which is a 129 | list composed of a body type and any number of body forms, each of 130 | which are evaluated in an environment where every specified slot in 131 | SLOTS as well as every specified supertype slot in SUPER is 132 | symbol-macrolet-bound to their respective name. The following body 133 | types are permitted: 134 | 135 | :COPY --- Provides the body forms for the COPY function. The 136 | generator instance is bound to GENERATOR. If this body 137 | expression is not provided, a copy function based on the 138 | SLOTS is automatically provided for you. 139 | This must be provided for HASH-GENERATORs. 140 | :RESEED --- Provides the body forms for the RESEED function. The 141 | generator instance is bound to GENERATOR and the seed to 142 | SEED. 143 | This must be provided for STATEFUL-GENERATORs. 144 | :NEXT --- Provides the body forms for the NEXT-BYTE function. The 145 | generator instance is bound to GENERATOR. Must return a 146 | suitable byte for the generator. 147 | This must be provided for STATEFUL-GENERATORs. 148 | :HASH --- Provides the stateless hashing function. The generator 149 | instance is notably NOT bound. However, the 64-bit index 150 | to hash is bound to INDEX. This will also automatically 151 | provide the NEXT-BYTE function for you. 152 | This must be provided for HASH-GENERATORs. 153 | 154 | Each of the additional bindings in the body expressions is bound to a 155 | symbol from the current package. 156 | 157 | See BITS-PER-BYTE 158 | See RESEED 159 | See NEXT-BYTE 160 | See HASH 161 | See COPY 162 | See HASH-GENERATOR 163 | See STATEFUL-GENERATOR 164 | See GENERATOR") 165 | 166 | (type stateful-generator 167 | "Superclass for all generators that rely on state to produce random numbers. 168 | 169 | See GENERATOR") 170 | 171 | (type hash-generator 172 | "Superclass for all generators that rely on a hashing function to generate numbers. 173 | 174 | These generators are special in that numbers for any index can be 175 | generated, so they can be rewound or arbitrarily stepped in their 176 | sequence. 177 | 178 | See GENERATOR 179 | See INDEX 180 | See REWIND") 181 | 182 | (function index 183 | "Accesses the index of the hash-generator. 184 | 185 | The index must be an (unsigned-byte 64). 186 | The index is advanced for each call to NEXT-BYTE. 187 | 188 | See HASH-GENERATOR") 189 | 190 | (function rewind 191 | "Rewind the hash-generator by BY numbers. 192 | 193 | The following invariant holds for any N: 194 | 195 | (= (next-byte generator) (progn (rewind generator N) (loop repeat (1- N) (next-byte generator)) (next-byte generator))) 196 | 197 | See HASH-GENERATOR")) 198 | 199 | ;; protocol.lisp 200 | (docs:define-docs 201 | (variable *generator* 202 | "The default random number generator used by RANDOM. 203 | 204 | See RANDOM") 205 | 206 | (function draw 207 | "Returns a vector with N random samples in [0,1[. 208 | 209 | See ENSURE-GENERATOR 210 | See RANDOM-UNIT") 211 | 212 | (function random 213 | "Returns a number in [0, MAX[. 214 | 215 | This is a drop-in replacement for CL:RANDOM. 216 | The returned type is the same as MAX, where MAX must be an INTEGER or 217 | a FLOAT greater than zero. The returned number must be smaller than MAX. 218 | 219 | GENERATOR may be anything accepted by ENSURE-GENERATOR. 220 | 221 | See RANDOM-INT 222 | See RANDOM-FLOAT 223 | See ENSURE-GENERATOR") 224 | 225 | (function random-1d 226 | "Returns a byte for the given index and seed. 227 | 228 | This is only usable with HASH-GENERATOR types. 229 | Does *NOT* advance the generator's index. 230 | 231 | See HASH-GENERATOR 232 | See NEXT-BYTE") 233 | 234 | (function random-2d 235 | "Returns a byte for the given location and seed. 236 | 237 | This is only usable with HASH-GENERATOR types. 238 | Does *NOT* advance the generator's index. 239 | 240 | See HASH-GENERATOR 241 | See NEXT-BYTE") 242 | 243 | (function random-3d 244 | "Returns a byte for the given location and seed. 245 | 246 | This is only usable with HASH-GENERATOR types. 247 | Does *NOT* advance the generator's index. 248 | 249 | See HASH-GENERATOR 250 | See NEXT-BYTE") 251 | 252 | (function random-byte 253 | "Alias for NEXT-BYTE. 254 | 255 | See GENERATOR 256 | See NEXT-BYTE") 257 | 258 | (function random-bytes 259 | "Returns an (UNSIGNED-BYTE BITS) sized random number. 260 | 261 | May advance the generator more than once. 262 | 263 | See GENERATOR 264 | See NEXT-BYTE") 265 | 266 | (function random-sequence 267 | "Fills SEQUENCE between START and END with random numbers. 268 | 269 | Note: it is up to you to ensure that SEQUENCE is capable of holding 270 | numbers returned by the generator's NEXT-BYTE, and that doing so makes 271 | sense. As in, do not fill a vector with element-type (unsigned-byte 8) 272 | with a generator whose BITS-PER-BYTE is 32 or vice-versa. 273 | 274 | Equivalent to: 275 | 276 | (map-into sequence (lambda () (next-byte generator))) 277 | 278 | See GENERATOR 279 | See NEXT-BYTE") 280 | 281 | (function random-unit 282 | "Returns a random float in [0, 1[. 283 | 284 | The returned float is of the type specified in TYPE. 285 | 286 | see GENERATOR 287 | See RANDOM-BYTES") 288 | 289 | (function random-float 290 | "Returns a random float in [FROM, TO[. 291 | 292 | The returned float is of the same type as whatever type is larger 293 | between FROM and TO. 294 | 295 | See GENERATOR 296 | See RANDOM-UNIT") 297 | 298 | (function random-int 299 | "Returns a random integer in [FROM, TO]. 300 | 301 | See GENERATOR 302 | See RANDOM-BYTES")) 303 | 304 | ;; toolkit.lisp 305 | (docs:define-docs 306 | (function hopefully-sufficiently-random-seed 307 | "Attempts to find a sufficiently random seed. 308 | 309 | On Unix, this reads 64 bits from /dev/urandom 310 | On Windows+SBCL, this reads 64 bits from SB-WIN32:CRYPT-GEN-RANDOM 311 | Otherwise it uses an XOR of GET-INTERNAL-REAL-TIME and GET-UNIVERSAL-TIME.") 312 | 313 | (function histogram 314 | "Compute a histogram from the given sample vector. 315 | 316 | This will collect the samples into N bins, where the value of the bin 317 | is the contribution of the samples in the bin towards all samples. 318 | 319 | See PRINT-HISTOGRAM 320 | See DRAW") 321 | 322 | (function print-histogram 323 | "Display the histogram vector in a user-friendly manner. 324 | 325 | Prints a visual representation of the deviation of each bin from the 326 | ideal uniform distribution as well as the cumulative deviation of all 327 | bins. 328 | 329 | See HISTOGRAM") 330 | 331 | (function benchmark 332 | "Draw a number of samples from an NRG and determine how quickly it operates. 333 | 334 | Prints the duration, # samples, samples/s, and s/sample to STREAM. 335 | Returns samples/s. 336 | 337 | See BENCHMARK-ALL") 338 | 339 | (function benchmark-all 340 | "Run a benchmark for all known generator types. 341 | 342 | When completed, orders the the results from fastest to slowest and 343 | prints them to STREAM. If a generator fails to be benchmarked, its 344 | result is shown as -1. 345 | 346 | See BENCHMARK")) 347 | 348 | ;; * RNGs 349 | (docs:define-docs 350 | (type adler32 351 | "An RNG based on the Adler32 hash. 352 | 353 | See https://en.wikipedia.org/wiki/Adler-32") 354 | 355 | (type cityhash-64 356 | "An RNG based on the 64-bit CityHash. 357 | 358 | See https://github.com/google/cityhash") 359 | 360 | (type hammersley 361 | "The Hammersley quasi-random number sequence. 362 | 363 | This is a multivariate generator with default dimensionality of 3. 364 | To change the dimensionality, pass a :LEAP initarg that is an array of 365 | the desired dimensionality. Each element in the LEAP array should be 366 | an integer greater or equal to 1, and can be used to advance the 367 | sequence more quickly for each dimension. 368 | 369 | See https://en.wikipedia.org/wiki/Low-discrepancy_sequence") 370 | 371 | (type kiss11 372 | "An implementation of the Kiss11 RNG. 373 | 374 | See https://eprint.iacr.org/2011/007.pdf") 375 | 376 | (type linear-congruence 377 | "A very simple random number generator based on linear congruence. 378 | 379 | See https://en.wikipedia.org/wiki/Linear_congruential_generator") 380 | 381 | (type mersenne-twister-32 382 | "The de-facto standard random number generator algorithm. 383 | 384 | See https://en.wikipedia.org/wiki/Mersenne_Twister 385 | See http://www.acclab.helsinki.fi/~knordlun/mc/mt19937.c") 386 | 387 | (type mersenne-twister-64 388 | "A 64 bit variant of the Mersenne Twister algorithm. 389 | 390 | See MERSENNE-TWISTER-32 391 | See http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/C-LANG/mt19937-64.c") 392 | 393 | (type middle-square 394 | "An incredibly primitive, and basically in practise useless, random number algorithm. 395 | 396 | See https://en.wikipedia.org/wiki/Middle-square_method") 397 | 398 | (type murmurhash3 399 | "A hash-based RNG using the Murmurhash3 method. 400 | 401 | See https://en.wikipedia.org/wiki/MurmurHash") 402 | 403 | (type pcg 404 | "An adaptation of the PCG rng. 405 | 406 | See http://www.pcg-random.org") 407 | 408 | (type quasi 409 | "A quasi-random number generator based on a uniform RNG. 410 | 411 | When constructing the generator you may pass the :SOURCE initarg, 412 | which should be a GENERATOR instance to use as a source of 413 | randomness. By default the current value of *GENERATOR* is used. 414 | 415 | See https://en.wikipedia.org/wiki/Low-discrepancy_sequence") 416 | 417 | (type rc4 418 | "The RC4 cryptographic random number generator. 419 | 420 | See https://en.wikipedia.org/wiki/RC4") 421 | 422 | (type sobol 423 | "The Sobol quasi-random number sequence. 424 | 425 | This is a multivariate generator with default dimensionality of 3. 426 | You can pass the :DIM initarg to specify the dimensionality of the 427 | result. :DIM must be in [ 1, 1111 [ 428 | 429 | See https://en.wikipedia.org/wiki/Low-discrepancy_sequence") 430 | 431 | (type squirrel 432 | "An adaptation of the \"squirrel hash v3\". 433 | 434 | See https://www.youtube.com/watch?v=LWFzPP8ZbdU") 435 | 436 | (type tt800 437 | "The predecessor to the Mersenne Twister algorithm. 438 | 439 | See http://random.mat.sbg.ac.at/publics/ftp/pub/data/tt800.c") 440 | 441 | (type xkcd 442 | "XKCD-221 random number generator 443 | 444 | See https://xkcd.com/221/") 445 | 446 | (type xorshift-32 447 | "Linear 32-bit word state shift-register generator. 448 | 449 | See https://en.wikipedia.org/wiki/Xorshift 450 | See https://www.jstatsoft.org/article/view/v008i14") 451 | 452 | (type xorshift-64 453 | "The 64-bit variant of the Xorshift algorithm.. 454 | 455 | See XORSHIFT-32") 456 | 457 | (type xorshift-128 458 | "The four times 32-bit word state variant of the Xorshift algorithm. 459 | 460 | See XORSHIFT-32") 461 | 462 | (type xorwow 463 | "Non-linear five times 32-bit word state shift-register generator. 464 | 465 | See XORSHIFT-128 466 | See https://en.wikipedia.org/wiki/Xorshift#xorwow") 467 | 468 | (type xorshift-64* 469 | "Non-linear variation of XORSHIFT-64 by adding a modulo multiplier. 470 | 471 | See XORSHIFT-64 472 | See https://en.wikipedia.org/wiki/Xorshift#xorshift*") 473 | 474 | (type xorshift-1024* 475 | "Sixteen 64-bit word state variation of XORSHIFT-64*. 476 | 477 | See XORSHIFT-64*") 478 | 479 | (type xorshift-128+ 480 | "Non-linear double 64-bit state variant of XORSHIFT-64 that's currently the standard on modern browsers' JavaScript engines. 481 | 482 | See XORSHIFT-64 483 | See https://en.wikipedia.org/wiki/Xorshift#xorshift+ 484 | See https://v8.dev/blog/math-random") 485 | 486 | (type xoshiro-128** 487 | "32-bit variant of XOSHIRO-256**. 488 | 489 | See XOSHIRO-256** 490 | See https://prng.di.unimi.it/xoshiro128starstar.c") 491 | 492 | (type xoshiro-128++ 493 | "32-bit variant of XOSHIRO-256++. 494 | 495 | See XOSHIRO-256++ 496 | See https://prng.di.unimi.it/xoshiro128plus.c") 497 | 498 | (type xoshiro-128+ 499 | "32-bit variant of XOSHIRO-256+. 500 | 501 | See XOSHIRO-256+ 502 | See https://prng.di.unimi.it/xoshiro128plus.c") 503 | 504 | (type xoshiro-256** 505 | "Non-linear rotating general-purpose 64-bit number algorithm. 506 | 507 | See https://prng.di.unimi.it/ 508 | See https://prng.di.unimi.it/xoshiro256starstar.c 509 | See https://en.wikipedia.org/wiki/Xorshift#xoshiro") 510 | 511 | (type xoshiro-256++ 512 | "A variant of XOSHIRO-256** using addition instead of multiplication. 513 | 514 | See XOSHIRO-256** 515 | See https://prng.di.unimi.it/xoshiro256plusplus.c") 516 | 517 | (type xoshiro-256+ 518 | "Slightly faster variant of XOSHIRO-256++ meant solely for generating 64-bit floating-point numbers by extracting the upper 53-bits due to the linearity of the lower bits. 519 | 520 | See XOSHIRO-256++ 521 | See https://prng.di.unimi.it/xoshiro256plus.c") 522 | 523 | (type xoroshiro-64* 524 | "Slightly faster variant of XOROSHIRO-64** meant solely for generating 32-bit floating-point numbers by extracting the upper 26-bits due to the linearity of the lower bits. 525 | 526 | See XOROSHIRO-64** 527 | See https://prng.di.unimi.it/xoroshiro64star.c") 528 | 529 | (type xoroshiro-64** 530 | "32-bit variant of XOROSHIRO-128**. 531 | 532 | See XOROSHIRO-128** 533 | See https://prng.di.unimi.it/xoroshiro64starstar.c") 534 | 535 | (type xoroshiro-128+ 536 | "A variant of XOROSHIRO-128++ that is slightly faster. It is suggested to be used for generating 64-bit floating-point values using its upper 53 bits and random boolean values using a sign test. 537 | 538 | See XOROSHIRO-128++ 539 | See https://prng.di.unimi.it/xoroshiro128plus.c") 540 | 541 | (type xoroshiro-128++ 542 | "A variant of XOROSHIRO-128** using addition instead of multiplication. 543 | 544 | See XOROSHIRO-128** 545 | See https://prng.di.unimi.it/xoroshiro128plusplus.c") 546 | 547 | (type xoroshiro-128** 548 | "Non-linear rotating general-purpose 64-bit number algorithm that is similar to Xoshiro-256** but uses less space. 549 | 550 | See XOSHIRO-256** 551 | See https://prng.di.unimi.it/ 552 | See https://prng.di.unimi.it/xoroshiro128starstar.c 553 | See https://en.wikipedia.org/wiki/Xoroshiro128%2B")) 554 | -------------------------------------------------------------------------------- /generator.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (defvar *generator-types* '(random-state)) 4 | (defvar *generators* (make-hash-table :test 'eql)) 5 | 6 | (defun global-generator (name) 7 | (or (gethash name *generators*) 8 | (setf (gethash name *generators*) 9 | (make-generator name)))) 10 | 11 | (defun (setf global-generator) (value name) 12 | (setf (gethash name *generators*) value)) 13 | 14 | #-allegro 15 | (define-compiler-macro global-generator (&whole whole name &environment env) 16 | (if (constantp name env) 17 | `(load-time-value (or (gethash ,name *generators*) 18 | (setf (gethash ,name *generators*) 19 | (make-generator ,name)))) 20 | whole)) 21 | 22 | (defmacro define-generator-fun (name (gen &rest args)) 23 | (let ((argsyms (loop for arg in args unless (find arg LAMBDA-LIST-KEYWORDS) collect arg))) 24 | `(progn (defgeneric ,name (,gen ,@args)) 25 | 26 | (define-compiler-macro ,name (&whole whole ,gen ,@args &environment env) 27 | (if (constantp ,gen env) 28 | `(,',name (ensure-generator ,,gen) ,,@argsyms) 29 | whole)) 30 | 31 | (defmethod ,name ((,gen symbol) ,@args) 32 | (,name (global-generator ,gen) ,@argsyms))))) 33 | 34 | (defun list-generator-types () 35 | (setf *generator-types* (sort *generator-types* #'string<))) 36 | 37 | (defstruct (generator 38 | (:constructor NIL) 39 | (:copier NIL) 40 | (:conc-name NIL)) 41 | (%seed 0 :type (unsigned-byte 64))) 42 | 43 | (defmethod print-object ((generator generator) stream) 44 | ;; Fully printing a state can be huge, so unless readability is requested, 45 | ;; use P-U-O to print something shorter. 46 | (if *print-readably* 47 | (call-next-method) 48 | (print-unreadable-object (generator stream :type T) 49 | (format stream "~s" (seed generator)))) 50 | ;; P-U-O does not return the object, which PRINT-OBJECT is supposed to do. 51 | generator) 52 | 53 | (defmethod make-load-form ((object generator) &optional env) 54 | (make-load-form-saving-slots object :environment env)) 55 | 56 | (defun ensure-generator (generator-ish) 57 | (etypecase generator-ish 58 | ((eql T) *generator*) 59 | (symbol (global-generator generator-ish)) 60 | (random-state generator-ish) 61 | (generator generator-ish))) 62 | 63 | (define-compiler-macro ensure-generator (&whole whole generator-ish &environment env) 64 | (if (constantp generator-ish env) 65 | `(etypecase ,generator-ish 66 | ((eql T) *generator*) 67 | (symbol (load-time-value (global-generator ,generator-ish))) 68 | (random-state ,generator-ish) 69 | (generator ,generator-ish)) 70 | whole)) 71 | 72 | (defgeneric %make-generator (type &key)) 73 | #+sbcl 74 | (locally (declare (sb-ext:muffle-conditions style-warning)) 75 | (define-generator-fun seed (generator))) 76 | #-sbcl (define-generator-fun seed (generator)) 77 | (define-generator-fun reseed (generator new-seed)) 78 | (define-generator-fun next-byte (generator)) 79 | (define-generator-fun bits-per-byte (generator)) 80 | (define-generator-fun copy (generator)) 81 | 82 | (defun multivariate-p (generator) 83 | (listp (bits-per-byte generator))) 84 | 85 | ;;; supporting methods for COPY 86 | (defmethod copy ((thing number)) 87 | thing) 88 | 89 | (defmethod copy ((thing array)) 90 | (make-array (array-dimensions thing) 91 | :element-type (array-element-type thing) 92 | :fill-pointer (array-has-fill-pointer-p thing) 93 | :adjustable (adjustable-array-p thing) 94 | :initial-contents thing)) 95 | 96 | 97 | (defun make-generator (type &optional (seed T) &rest initargs) 98 | (let ((generator (apply #'%make-generator type initargs))) 99 | (when seed (reseed generator seed)) 100 | generator)) 101 | 102 | (defmethod reseed ((generator generator) (new-seed (eql T))) 103 | (reseed generator (hopefully-sufficiently-random-seed)) 104 | generator) 105 | 106 | (defmethod seed ((generator generator)) 107 | (%seed generator)) 108 | 109 | (defmacro define-generator (name bits-per-byte super slots &body bodies) 110 | (let* ((constructor (intern* 'make name)) 111 | (copy (intern* 'copy name)) 112 | (reseed (intern* name 'reseed)) 113 | (next (intern* name 'next)) 114 | (hash (intern* name 'hash)) 115 | (generator (intern* 'generator)) 116 | (bindings (append (loop for (slot) in slots collect 117 | `(,slot (,(intern* name slot) ,generator))) 118 | (loop for (slot) in (rest super) collect 119 | `(,slot (,(intern* (first super) slot) ,generator))))) 120 | (seed (intern* 'seed)) 121 | (index (intern* 'index))) 122 | `(progn 123 | (pushnew ',name *generator-types*) 124 | 125 | (defstruct (,name 126 | (:include ,@super) 127 | (:constructor ,constructor) 128 | (:copier NIL) 129 | (:predicate NIL)) 130 | ,@slots) 131 | 132 | ,@(loop for (type . body) in bodies 133 | collect (ecase type 134 | (:copy 135 | `(defun ,copy (,generator) 136 | ,@body)) 137 | (:reseed 138 | `(progn (defun ,reseed (,generator ,seed) 139 | (setf (%seed ,generator) ,seed) 140 | (symbol-macrolet ,bindings 141 | ,@body)) 142 | (defmethod reseed ((,generator ,name) (new-seed integer)) 143 | (,reseed ,generator new-seed)))) 144 | (:next 145 | `(progn (defun ,next (,generator) 146 | ;; sometimes argument is not used. 147 | (declare (ignorable ,generator)) 148 | (symbol-macrolet ,bindings 149 | ,@body)) 150 | (defmethod next-byte ((,generator ,name)) 151 | (,next ,generator)) 152 | (defmethod next-byte-fun ((,generator ,name)) 153 | #',next))) 154 | (:hash 155 | `(progn (defun ,hash (,index ,seed ,@(mapcar #'first bindings)) 156 | (declare (type (unsigned-byte 64) ,index ,seed)) 157 | (declare ,@(loop for (slot nil . args) in slots 158 | collect `(type ,(getf args :type T) ,slot))) 159 | ,@body) 160 | (defmethod hash ((,generator ,name) ,index ,seed) 161 | (,hash ,index ,seed ,@(mapcar #'second bindings))) 162 | (defun ,next (,generator) 163 | (let ((index (fit-bits 64 (1+ (,(intern* name 'index) ,generator)))) 164 | (seed (,(intern* name '%seed) ,generator))) 165 | (setf (,(intern* name 'index) ,generator) index) 166 | (symbol-macrolet ,bindings 167 | (locally 168 | ,@body)))) 169 | (defmethod next-byte ((,generator ,name)) 170 | (,next ,generator)) 171 | (defmethod next-byte-fun ((,generator ,name)) 172 | #',next))))) 173 | 174 | ,@(unless (find :copy bodies :key #'car) 175 | `((defun ,copy (,generator) 176 | ;; sometimes the argument is not used. 177 | (declare (ignorable ,generator)) 178 | (,constructor ,@(loop for binding in bindings 179 | collect (intern (string (first binding)) "KEYWORD") 180 | collect `(copy ,(second binding))))))) 181 | (defmethod copy ((generator ,name)) 182 | (,copy generator)) 183 | (defmethod %make-generator ((type (eql ',name)) &rest initargs &key &allow-other-keys) 184 | (apply #',constructor initargs)) 185 | (defmethod bits-per-byte ((generator ,name)) 186 | ,bits-per-byte)))) 187 | 188 | (defstruct (stateful-generator 189 | (:include generator) 190 | (:constructor NIL) 191 | (:copier NIL) 192 | (:predicate NIL))) 193 | 194 | (defmethod %make-generator :around ((type stateful-generator) &key) 195 | (let ((generator (call-next-method))) 196 | (reseed generator 0) 197 | generator)) 198 | 199 | (defmethod %make-generator ((type symbol) &rest args) 200 | (let ((name (find-symbol (string type) #.*package*))) 201 | (if (and name (member name *generator-types*)) 202 | (apply #'%make-generator name args) 203 | (error "No generator with name ~s known." type)))) 204 | 205 | (defstruct (hash-generator 206 | (:include generator) 207 | (:constructor NIL) 208 | (:copier NIL) 209 | (:predicate NIL) 210 | (:conc-name NIL)) 211 | (index 0 :type (unsigned-byte 64))) 212 | 213 | (define-generator-fun rewind (hash-generator &optional by)) 214 | (define-generator-fun hash (hash-generator index seed)) 215 | 216 | (defmethod reseed ((generator hash-generator) (seed integer)) 217 | (setf (index generator) 0) 218 | (setf (%seed generator) (fit-bits 64 seed))) 219 | 220 | (defmethod rewind ((generator hash-generator) &optional (by 1)) 221 | (setf (index generator) (fit-bits 64 (- (index generator) by)))) 222 | -------------------------------------------------------------------------------- /hammersley.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator hammersley (make-list (length (hammersley-leap generator)) :initial-element 'single-float) (hash-generator) 4 | ((leap (make-array 3 :element-type '(unsigned-byte 32) :initial-element 1) :type (simple-array (unsigned-byte 32) (*)))) 5 | (:copy 6 | (make-hammersley :%seed (hammersley-%seed generator) 7 | :index (hammersley-index generator) 8 | :leap (make-array (length (hammersley-leap generator)) 9 | :element-type '(unsigned-byte 32) 10 | :initial-contents (hammersley-leap generator)))) 11 | (:hash 12 | (declare (optimize speed)) 13 | (flet ((dim (base leap seed) 14 | (declare (type (unsigned-byte 32) base leap seed)) 15 | (let ((seed2 (fit-bits 32 (+ seed (* index leap)))) 16 | (base-inv (/ (float base 0f0))) 17 | (r 0f0)) 18 | (declare (type single-float r base-inv)) 19 | (loop while (/= 0 seed2) 20 | do (incf r (* (mod seed2 base) base-inv)) 21 | (setf base-inv (/ base-inv base)) 22 | (setf seed2 (truncate seed2 base)) 23 | finally (return r))))) 24 | (let ((result (make-array (length leap) :element-type 'single-float))) 25 | (declare (type (simple-array (unsigned-byte 32) (*)) leap)) 26 | (dotimes (i (length result) result) 27 | (setf (aref result i) (dim (prime (1+ i)) (aref leap i) (ldb (byte 8 i) seed)))))))) 28 | -------------------------------------------------------------------------------- /histogram.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (defun histogram (rng bins &key (samples (floor 1e8)) (width 80) (stream *standard-output*) (print-summary T)) 4 | (check-type samples (unsigned-byte 64)) 5 | (let ((histogram (make-array bins)) 6 | (sample-contribution (/ samples)) 7 | (start (get-internal-real-time))) 8 | (format stream "0% ") 9 | (flet ((percentage (i) 10 | (round (* (- width 8) (/ i samples))))) 11 | (dotimes (i samples) 12 | (when (/= (percentage i) (percentage (1+ i))) 13 | (format stream "█")) 14 | (locally (declare (optimize speed)) 15 | (incf (aref histogram (floor (* (random 1.0 rng) bins))) 16 | sample-contribution)))) 17 | (format stream " 100%~%") 18 | (when print-summary 19 | (let ((duration (/ (- (get-internal-real-time) start) 20 | INTERNAL-TIME-UNITS-PER-SECOND))) 21 | (format stream "Generation took: ~6,3fs, ~fμs/sample~%" 22 | duration (* 1000000 (/ duration samples))))) 23 | histogram)) 24 | 25 | (defun histogram-deviation (histogram) 26 | (loop for bin across histogram 27 | for deviation = (* 100 (- bin (/ (length histogram)))) 28 | sum (abs deviation))) 29 | 30 | (defun print-histogram (histogram &key (stream *standard-output*) (width 80)) 31 | (assert (< 7 width)) 32 | (let ((half-width (/ (- width 7) 2)) 33 | (total 0.0)) 34 | (loop for bin across histogram 35 | for deviation = (* 100 (- bin (/ (length histogram)))) 36 | for chars = (max (- half-width) (min (+ half-width) (floor (* 2 half-width deviation)))) 37 | do (format stream "~6,3@f% ~v@{░~}~v@{█~}~v@{░~}~%" deviation 38 | (min half-width (+ half-width chars)) 39 | (abs chars) 40 | (min half-width (- half-width chars)) 41 | NIL) 42 | (incf total (abs deviation))) 43 | (format stream "Cumulative deviation: ~6,3f%~%" total) 44 | histogram)) 45 | 46 | (defun histogram-all (&key (bins 5) (samples (floor 1e6)) (width 80) (stream *standard-output*)) 47 | (format stream "Generating samples...~%") 48 | (let* ((stats (loop for rng in (list-generator-types) 49 | for result = (ignore-errors 50 | (format stream "~&~19a " rng) 51 | (cons rng (histogram rng bins :samples samples :width (- width 20) :print-summary NIL))) 52 | when result collect result))) 53 | (setf stats (sort stats #'< :key (lambda (x) (histogram-deviation (cdr x))))) 54 | (loop for (rng . histogram) in stats 55 | do (format stream "~&~%~a~%" rng) 56 | (print-histogram histogram :stream stream :width width)))) 57 | 58 | (defun benchmark (rng &key (samples 1e6) (stream *standard-output*)) 59 | ;; this declaration is necessary because ENSURE-GENERATOR is a 60 | ;; forward-reference and SBCL does not like it that it misses the 61 | ;; chance to apply the compiler macro here. [2024/07/29:rpg] 62 | (let* ((rng (ensure-generator rng)) 63 | (next-fun (next-byte-fun rng)) 64 | (start (get-internal-run-time))) 65 | (declare (type (unsigned-byte 64) samples)) 66 | (declare (type (function (generator) T) next-fun)) 67 | (locally (declare (optimize speed (safety 0))) 68 | (loop repeat samples 69 | do (funcall next-fun rng))) 70 | (let* ((end (get-internal-run-time)) 71 | (duration (float (/ (- end start) INTERNAL-TIME-UNITS-PER-SECOND) 0d0))) 72 | (format stream "Duration: ~12t~10,1f~%Samples: ~12t~10d~%Samples/s: ~12t~10,1f~%S/sample: ~12t~10,8f" 73 | duration samples (/ samples duration) (/ duration samples)) 74 | (/ samples duration)))) 75 | 76 | (defun benchmark-all (&key (samples 1e6) (stream *standard-output*)) 77 | (let* ((nullstream (make-broadcast-stream)) 78 | (stats (loop for rng in (list-generator-types) 79 | for result = (cons rng (handler-case (benchmark rng :samples samples :stream nullstream) 80 | (error () -1))) 81 | when result collect result))) 82 | (setf stats (sort stats #'> :key #'cdr)) 83 | (format stream "RNG~20tSamples/second~%") 84 | (loop for (rng . samples) in stats 85 | do (format stream "~&~a~20t~18,1f~%" rng samples)))) 86 | -------------------------------------------------------------------------------- /implementation.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | ;;;; Integration with the implementation. 4 | 5 | (setf (global-generator :system) *random-state*) 6 | 7 | (defmethod %make-generator ((type (eql 'random-state)) &key) 8 | (make-random-state T)) 9 | 10 | (defmethod seed ((generator random-state)) 11 | #-(or) 12 | (error "Can't retrieve seed from RANDOM-STATE objects on ~a" (lisp-implementation-type))) 13 | 14 | (defmethod reseed ((generator random-state) seed) 15 | #+sbcl 16 | (let ((seeded (sb-ext:seed-random-state seed))) 17 | (replace (sb-kernel::random-state-state generator) 18 | (sb-kernel::random-state-state seeded))) 19 | #+allegro 20 | (setf (excl::random-state-seed generator) seed) 21 | #-(or allegro sbcl) (declare (ignorable seed)) 22 | #-(or allegro sbcl) (warn "Can't reseed RANDOM-STATE objects on ~a" (lisp-implementation-type)) 23 | generator) 24 | 25 | (defmethod next-byte ((generator random-state)) 26 | (cl:random most-positive-fixnum generator)) 27 | 28 | (defmethod next-byte-fun ((generator random-state)) 29 | (lambda (generator) (cl:random most-positive-fixnum generator))) 30 | 31 | (defmethod bits-per-byte ((generator random-state)) 32 | (integer-length most-positive-fixnum)) 33 | 34 | (defmethod copy ((generator random-state)) 35 | (make-random-state generator)) 36 | -------------------------------------------------------------------------------- /kiss.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator kiss11 32 (stateful-generator) 4 | ((q (32bit-seed-array 4194304 0) :type (simple-array (unsigned-byte 32) (4194304))) 5 | (carry 0 :type (unsigned-byte 32)) 6 | (j 4194303 :type (unsigned-byte 32)) 7 | (cng 123456789 :type (unsigned-byte 32)) 8 | (xs 362436069 :type (unsigned-byte 32))) 9 | (:reseed 10 | (setf q (32bit-seed-array 4194304 seed))) 11 | (:next 12 | (setf cng (fit-bits 32 (+ (* cng 69069) 13579))) 13 | (update 32 xs logxor (ash xs +13)) 14 | (update 32 xs logxor (ash xs -17)) 15 | (update 32 xs logxor (ash xs +5)) 16 | (setf j (logand (1+ j) 4194303)) 17 | (let* ((x (aref q j)) 18 | (tt (fit-bits 32 (+ carry (ash x 28))))) 19 | (setf carry (- (ash x -4) (if (< tt x) 1 0))) 20 | (setf (aref q j) (ldb (byte 32 0) (- tt x)))) 21 | (+ (aref q j) cng xs))) 22 | -------------------------------------------------------------------------------- /linear-congruence.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator linear-congruence 64 (stateful-generator) 4 | ((state 0 :type (unsigned-byte 64)) 5 | (multiplier 6364136223846793005 :type (unsigned-byte 64)) 6 | (increment 1442695040888963407 :type (unsigned-byte 64))) 7 | (:reseed 8 | (setf state (mod seed (1- (ash 1 64))))) 9 | (:next 10 | (declare (optimize speed)) 11 | (update 64 state * multiplier) 12 | (update 64 state + increment))) 13 | -------------------------------------------------------------------------------- /mersenne-twister.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | ;; Adapted from 4 | ;; http://www.acclab.helsinki.fi/~knordlun/mc/mt19937.c 5 | ;; and 6 | ;; http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/C-LANG/mt19937-64.c 7 | ;; respectively. 8 | 9 | (defstruct (mersenne-twister 10 | (:include stateful-generator) 11 | (:constructor NIL) 12 | (:predicate NIL)) 13 | (index 0 :type (unsigned-byte 64))) 14 | 15 | (defmacro %inner-mersenne-twister (bytes n m upper lower matrix magic &rest shiftops) 16 | `(let ((i 0) 17 | (n ,n) 18 | (m ,m) 19 | (upper ,upper) 20 | (lower ,lower) 21 | (matrix ,matrix) 22 | (magic ,magic)) 23 | (declare (optimize speed) 24 | (type (simple-array (unsigned-byte ,bytes)) matrix magic) 25 | (type (unsigned-byte 16) n m i)) 26 | (flet ((magic (i) (aref magic i)) 27 | (matrix (i) (aref matrix i))) 28 | (declare (inline magic matrix)) 29 | (when (= (mersenne-twister-index generator) n) 30 | (loop while (< i (- n m)) 31 | do (let ((x (logior (logand (matrix i) upper) 32 | (logand (matrix (1+ i)) lower)))) 33 | (setf (aref matrix i) 34 | (logxor (matrix (+ i m)) 35 | (ash x -1) 36 | (magic (mod x 2))))) 37 | (incf i)) 38 | (loop while (< i (1- n)) 39 | do (let ((x (logior (logand (matrix i) upper) 40 | (logand (matrix (1+ i)) lower)))) 41 | (setf (aref matrix i) 42 | (logxor (matrix (+ i (- m n))) 43 | (ash x -1) 44 | (magic (mod x 2))))) 45 | (incf i)) 46 | (setf (mersenne-twister-index generator) 0)) 47 | (let ((result (matrix (mersenne-twister-index generator)))) 48 | (declare (type (unsigned-byte ,bytes) result)) 49 | (setf (mersenne-twister-index generator) (logand (1+ (mersenne-twister-index generator)) (1- (ash 1 64)))) 50 | ,@(loop for (shift mask) in shiftops 51 | collect `(setf result (logxor result 52 | (logand (ash (the (unsigned-byte ,bytes) result) 53 | ,shift) 54 | ,mask)))) 55 | result)))) 56 | 57 | (define-generator mersenne-twister-32 32 (mersenne-twister (index 624)) 58 | ((upper #x80000000 :type (unsigned-byte 32)) 59 | (lower #x7fffffff :type (unsigned-byte 32)) 60 | (magic (barr 32 0 #x9908b0df) :type (simple-array (unsigned-byte 32) (2))) 61 | (matrix (make-array 624 :element-type `(unsigned-byte 32)) :type (simple-array (unsigned-byte 32) (624)))) 62 | (:reseed 63 | (setf matrix (32bit-seed-array 624 seed)) 64 | (setf index 624)) 65 | (:next 66 | (%inner-mersenne-twister 32 624 397 upper lower matrix magic 67 | (-11 #xFFFFFFFF) 68 | ( 7 #x9D2C5680) 69 | ( 15 #xEFC60000) 70 | (-18 #xFFFFFFFF)))) 71 | 72 | (define-generator mersenne-twister-64 64 (mersenne-twister (index 312)) 73 | ((upper #xFFFFFFFF80000000 :type (unsigned-byte 64)) 74 | (lower #x000000007FFFFFFF :type (unsigned-byte 64)) 75 | (magic (barr 64 0 #xB5026F5AA96619E9) :type (simple-array (unsigned-byte 64) (2))) 76 | (matrix (make-array 312 :element-type `(unsigned-byte 64)) :type (simple-array (unsigned-byte 64) (312)))) 77 | (:reseed 78 | (setf matrix (64bit-seed-array 312 seed)) 79 | (setf index 312)) 80 | (:next 81 | (%inner-mersenne-twister 64 312 156 upper lower matrix magic 82 | (-29 #x5555555555555555) 83 | ( 17 #x71D67FFFEDA60000) 84 | ( 37 #xFFF7EEE000000000) 85 | (-43 #xFFFFFFFFFFFFFFFF)))) 86 | -------------------------------------------------------------------------------- /middle-square.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | 4 | (define-generator middle-square (middle-square-bits generator) (stateful-generator) 5 | ((bits 64 :type (unsigned-byte 8)) 6 | (state 0 :type unsigned-byte)) 7 | (:reseed 8 | (setf state seed) 9 | (setf bits (integer-length seed))) 10 | (:next 11 | (let* ((square (expt state 2)) 12 | (offset (floor (max 0 (- (integer-length square) bits)) 2)) 13 | (new (ldb (byte bits offset) square))) 14 | (setf state new)))) 15 | -------------------------------------------------------------------------------- /murmurhash.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator murmurhash3 32 (hash-generator) () 4 | (:copy 5 | (make-murmurhash3 :%seed (murmurhash3-%seed generator) 6 | :index (murmurhash3-index generator))) 7 | (:hash 8 | (declare (optimize speed (safety 1))) 9 | (flet ((rotl (x r) 10 | (fit-bits 32 (logior (ash x r) (ash x (- (- 32 r)))))) 11 | (fmix (h) 12 | (update 32 h logxor (ash h -16)) 13 | (update 32 h * #x85ebca6b) 14 | (update 32 h logxor (ash h -13)) 15 | (update 32 h * #xc2b2ae35) 16 | (update 32 h logxor (ash h -16)))) 17 | (declare (inline rotl fmix)) 18 | (let ((h1 seed) 19 | (c1 #xcc9e2d51) 20 | (c2 #x1b873593) 21 | (k1 (fit-bits 32 index)) 22 | (len 4)) 23 | (update 32 k1 * c1) 24 | (update 32 k1 rotl 15) 25 | (update 32 k1 * c2) 26 | (update 32 h1 logxor k1) 27 | (update 32 h1 rotl 13) 28 | (setf h1 (fit-bits 32 (+ (* 5 h1) #xe6546b64))) 29 | (update 32 h1 logxor len) 30 | (fmix h1))))) 31 | -------------------------------------------------------------------------------- /package.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:cl-user) 2 | (defpackage #:random-state 3 | (:nicknames #:org.shirakumo.random-state) 4 | (:use #:cl) 5 | (:shadow #:random) 6 | ;; generator.lisp 7 | (:export 8 | #:*generator* 9 | #:global-generator 10 | #:ensure-generator 11 | #:list-generator-types 12 | #:generator 13 | #:seed 14 | #:reseed 15 | #:next-byte 16 | #:bits-per-byte 17 | #:multivariate-p 18 | #:copy 19 | #:make-generator 20 | #:define-generator 21 | #:stateful-generator 22 | #:hash-generator 23 | #:index 24 | #:rewind) 25 | ;; protocol.lisp 26 | (:export 27 | #:draw 28 | #:random 29 | #:random-1d 30 | #:random-2d 31 | #:random-3d 32 | #:random-byte 33 | #:random-bytes 34 | #:random-sequence 35 | #:random-unit 36 | #:random-float 37 | #:random-int) 38 | ;; toolkit.lisp 39 | (:export 40 | #:hopefully-sufficiently-random-seed 41 | #:histogram 42 | #:print-histogram 43 | #:histogram-all 44 | #:benchmark 45 | #:benchmark-all) 46 | ;; * rngs 47 | (:export 48 | #:adler32 49 | #:cityhash-64 50 | #:hammersley 51 | #:kiss11 52 | #:linear-congruence 53 | #:mersenne-twister-32 54 | #:mersenne-twister-64 55 | #:middle-square 56 | #:murmurhash3 57 | #:pcg 58 | #:quasi 59 | #:rc4 60 | #:sobol 61 | #:squirrel 62 | #:tt800 63 | #:xkcd 64 | #:xorshift-1024* 65 | #:xorshift-128 66 | #:xorshift-128+ 67 | #:xorshift-32 68 | #:xorshift-64 69 | #:xorshift-64* 70 | #:xorwow 71 | #:xoshiro-128** 72 | #:xoshiro-128+ 73 | #:xoshiro-256** 74 | #:xoshiro-256+)) 75 | -------------------------------------------------------------------------------- /pcg.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator pcg 32 (stateful-generator) 4 | ((state #x853c49e6748fea9b :type (unsigned-byte 64)) 5 | (inc #xda3e39cb94b95bdb :type (unsigned-byte 64))) 6 | (:reseed 7 | (setf state 0) 8 | (setf inc (fit-bits 64 (logior 1 (ash seed 1)))) 9 | (pcg-next generator) 10 | (setf state (fit-bits 64 (+ state seed))) 11 | (pcg-next generator)) 12 | (:next 13 | (declare (optimize speed)) 14 | (let ((oldstate state)) 15 | (setf state (fit-bits 64 (+ (fit-bits 64 (* oldstate 6364136223846793005)) inc))) 16 | (let ((xord (fit-bits 32 (ash (logxor (ash oldstate -18) oldstate) -27))) 17 | (rot (fit-bits 32 (ash oldstate -59)))) 18 | (fit-bits 32 (logior (ash xord (- rot)) (ash xord (logand (- rot) 31)))))))) 19 | 20 | -------------------------------------------------------------------------------- /primes.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (let ((pis-1 (make-array 0 :adjustable T :fill-pointer T :element-type '(signed-byte 64))) 4 | (pis (make-array 0 :adjustable T :fill-pointer T :element-type '(unsigned-byte 64))) 5 | (primes (make-array 1 :adjustable T :fill-pointer T :element-type '(unsigned-byte 64) :initial-contents '(2)))) 6 | (defun compute-ruiz-pis-part1 (j) 7 | (if (< j (length pis-1)) 8 | (aref pis-1 j) 9 | (let* ((s-max (floor (sqrt j))) 10 | (sigma1 (loop for s from 1 to s-max 11 | sum (- (floor (/ (1- j) s)) (floor (/ j s))))) 12 | (result (floor (* (/ 2 j) (1+ sigma1))))) 13 | (vector-push-extend result pis-1) 14 | result))) 15 | 16 | (defun compute-ruiz-pi (k) 17 | (if (< k (length pis)) 18 | (aref pis k) 19 | (let ((result (cond 20 | ((= k 1) 0) 21 | ((= k 2) (1+ (compute-ruiz-pis-part1 2))) 22 | (T (+ 1 (compute-ruiz-pis-part1 k) (compute-ruiz-pi (1- k))))))) 23 | (vector-push-extend result pis) 24 | result))) 25 | 26 | (defun prime (n) 27 | (declare (type (unsigned-byte 32) n)) 28 | (if (< n (length primes)) 29 | (aref primes n) 30 | (let* ((n (1+ n)) 31 | (result (1+ (loop for k from 1 to (* 2 (1+ (floor (* n (log n))))) 32 | summing (- 1 (floor (/ (compute-ruiz-pi k) n))))))) 33 | (vector-push-extend result primes) 34 | result)))) 35 | -------------------------------------------------------------------------------- /protocol.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (defvar *generator* *random-state*) 4 | 5 | ;; FIXME: these do not work correctly if RANDOM-BYTES fails because RANDOM-BYTE returns 6 | ;; a non-integer, as it may do for pseudo generators. 7 | (define-compiler-macro random-float (&whole whole generator from to &environment env) 8 | (cond ((and (constantp from env) (constantp to env)) 9 | `(+ (load-time-value (min ,to ,from)) 10 | (* (load-time-value (abs (- ,to ,from))) 11 | (scale-float (float (random-bytes ,generator (load-time-value (float-digits (- ,to ,from)))) ,from) 12 | (load-time-value (- (float-digits (- ,to ,from)))))))) 13 | ((constantp from env) 14 | `(let ((from ,from) 15 | (to ,to)) 16 | (when (< to from) 17 | (rotatef from to)) 18 | (+ from 19 | (* (- to from) 20 | (scale-float (float (random-bytes ,generator (load-time-value (float-digits ,from))) ,from) 21 | (load-time-value (- (float-digits ,from)))))))) 22 | ((constantp to env) 23 | `(let ((from ,from) 24 | (to ,to)) 25 | (when (< to from) 26 | (rotatef from to)) 27 | (+ from 28 | (* (- to from) 29 | (scale-float (float (random-bytes ,generator (load-time-value (float-digits ,to))) ,to) 30 | (load-time-value (- (float-digits ,to)))))))) 31 | (T 32 | whole))) 33 | 34 | (define-compiler-macro random-unit (&whole whole generator &optional (type ''single-float) &environment env) 35 | (if (constantp type env) 36 | `(scale-float (coerce (random-bytes ,generator (load-time-value (float-digits (coerce 0 ,type)))) ,type) 37 | (load-time-value (- (float-digits (coerce 0 ,type))))) 38 | whole)) 39 | 40 | (declaim (inline random)) 41 | 42 | (defun random (max &optional (generator *generator*)) 43 | (macrolet ((gen (&rest types) 44 | `(etypecase max 45 | ((integer 0) 46 | (random-int generator 0 (1- max))) 47 | ,@(loop for (type alias) in types 48 | for zero = (coerce 0 type) 49 | unless (and alias (subtypep type alias)) 50 | collect `((,type ,zero) 51 | (random-float generator ,zero max)))))) 52 | (gen (short-float single-float) 53 | (single-float) 54 | (double-float) 55 | (long-float double-float)))) 56 | 57 | (defun draw (n &optional (generator *generator*)) 58 | (let ((samples (make-array n :element-type 'single-float)) 59 | (generator (ensure-generator generator))) 60 | (map-into samples (lambda () (random-unit generator))))) 61 | 62 | (defun random-1d (generator index &optional (seed 0)) 63 | (hash generator index seed)) 64 | 65 | (defun random-2d (generator x y &optional (seed 0)) 66 | (hash generator (+ x (* y 198491317)) seed)) 67 | 68 | (defun random-3d (generator x y z &optional (seed 0)) 69 | (hash generator (+ x (* y 198491317) (* z 6542989)) seed)) 70 | 71 | (defun random-byte (generator) 72 | (next-byte generator)) 73 | 74 | (declaim (ftype (function (T (integer 0)) (integer 0)) random-bytes)) 75 | (defun random-bytes (generator bits) 76 | (declare (optimize speed)) 77 | (let* ((generator (ensure-generator generator)) 78 | (chunk (bits-per-byte generator))) 79 | (declare (type (unsigned-byte 8) bits)) 80 | (cond ((not (integerp chunk)) 81 | (error "This generator does not output bits of randomness.~% ~a~%generates~% ~a" 82 | generator chunk)) 83 | ((= bits chunk) 84 | (next-byte generator)) 85 | ((< bits chunk) 86 | (fit-bits bits (next-byte generator))) 87 | (T 88 | (let ((random 0)) 89 | (declare (type (unsigned-byte 8) chunk)) 90 | ;; Fill upper bits 91 | (loop for i downfrom (- bits chunk) above 0 by chunk 92 | for byte = (next-byte generator) 93 | do (setf (ldb (byte chunk i) random) byte)) 94 | ;; Fill lowermost bits. 95 | ;; This will cause spilling on misaligned boundaries, but we don't care. 96 | (setf (ldb (byte chunk 0) random) (next-byte generator)) 97 | random))))) 98 | 99 | (defun random-sequence (generator sequence &key (start 0) (end (length sequence))) 100 | (let ((generator (ensure-generator generator))) 101 | (etypecase sequence 102 | (list 103 | (loop for cons on sequence 104 | do (setf (car cons) (next-byte generator)))) 105 | (vector 106 | (loop for i from start below end 107 | do (setf (aref sequence i) (next-byte generator)))) 108 | (sequence 109 | (map-into sequence (lambda () (next-byte generator))))))) 110 | 111 | (defun random-unit (generator &optional (type 'single-float)) 112 | (etypecase (bits-per-byte generator) 113 | ((member single-float double-float) 114 | (coerce (random-byte generator) type)) 115 | (integer 116 | (let* ((bits (float-digits (coerce 0 type))) 117 | (random (random-bytes generator bits))) 118 | ;; KLUDGE: this sucks. Would be better if we could directly fill the mantissa. 119 | (scale-float (coerce random type) (- bits)))))) 120 | 121 | (defun random-float (generator from to) 122 | (let ((from from) 123 | (to to)) 124 | (when (< to from) 125 | (rotatef from to)) 126 | (+ from (* (- to from) (random-unit generator (type-of from)))))) 127 | 128 | (defun random-int (generator from to) 129 | (declare (optimize speed)) 130 | (declare (type integer from to)) 131 | (when (< to from) 132 | (rotatef from to)) 133 | (let* ((generator (ensure-generator generator)) 134 | (range (- to from)) 135 | (bits (integer-length range))) 136 | (+ from 137 | ;; CANDIDATE is in the range [0, 2^BITS). Trying to map 138 | ;; this to our target range will introduce bias -- for example, 139 | ;; in the [0, 2] case, you could map 0->0, 1->0, 2->1, 3->2 and 0 would 140 | ;; come up twice as often as 1 & 2. So we instead re-roll until we get 141 | ;; a candidate in our range. Bigger range = fewer expected rolls. 142 | ;; But even in the 0..2 case, the expected number of rolls is still only ~1.33: 143 | ;; E[R] = (3/4)1 + (1/4)(1 + E[R]), 144 | ;; 4E[R] = 3 + 1 + E[R], 145 | ;; E[R] = 4/3 = ~1.33. 146 | (loop for candidate = (random-bytes generator bits) 147 | when (<= candidate range) 148 | return candidate)))) 149 | 150 | -------------------------------------------------------------------------------- /quasi.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator quasi 'single-float (stateful-generator) 4 | ((source *generator* :type T) 5 | (last 0.0 :type single-float)) 6 | (:reseed 7 | (reseed source seed) 8 | (setf last 0.0)) 9 | (:next 10 | (declare (optimize speed (safety 1))) 11 | (setf last (mod (+ last 0.5 (random-unit source)) 1.0)))) 12 | 13 | ;; TODO: multivariate 14 | -------------------------------------------------------------------------------- /random-state.asd: -------------------------------------------------------------------------------- 1 | (asdf:defsystem random-state 2 | :version "1.0.1" 3 | :author "Yukari Hafner " 4 | :license "zlib" 5 | :description "Portable random number generation." 6 | :homepage "https://Shinmera.github.io/random-state/" 7 | :bug-tracker "https://github.com/Shinmera/random-state/issues" 8 | :source-control (:git "https://github.com/Shinmera/random-state.git") 9 | :in-order-to ((test-op (test-op "random-state-test"))) 10 | :serial T 11 | :components ((:file "package") 12 | (:file "toolkit") 13 | (:file "generator") 14 | (:file "protocol") 15 | (:file "primes") 16 | (:file "adler32") 17 | (:file "cityhash") 18 | (:file "hammersley") 19 | (:file "linear-congruence") 20 | (:file "kiss") 21 | (:file "mersenne-twister") 22 | (:file "middle-square") 23 | (:file "murmurhash") 24 | (:file "pcg") 25 | (:file "quasi") 26 | (:file "rc4") 27 | (:file "sobol") 28 | (:file "squirrel") 29 | (:file "tt800") 30 | (:file "xkcd") 31 | (:file "xorshift") 32 | (:file "implementation") 33 | (:file "histogram") 34 | (:file "documentation")) 35 | :depends-on (:documentation-utils) 36 | :in-order-to ((asdf:test-op (asdf:test-op :random-state-test)))) 37 | 38 | (asdf:defsystem random-state/test 39 | :version "1.0.0" 40 | :author "Yukari Hafner " 41 | :license "zlib" 42 | :description "Portable random number generation." 43 | :homepage "https://Shinmera.github.io/random-state/" 44 | :bug-tracker "https://github.com/Shinmera/random-state/issues" 45 | :source-control (:git "https://github.com/Shinmera/random-state.git") 46 | :serial T 47 | :components ((:file "test")) 48 | :depends-on (:random-state :parachute) 49 | :perform (asdf:test-op (op c) (uiop:symbol-call :parachute :test :org.shirakumo.random-state.test))) 50 | 51 | (asdf:defsystem random-state/viewer 52 | :version "1.0.0" 53 | :author "Yukari Hafner " 54 | :license "zlib" 55 | :description "Visualiser for the random number generators" 56 | :homepage "https://Shinmera.github.io/random-state/" 57 | :bug-tracker "https://github.com/Shinmera/random-state/issues" 58 | :source-control (:git "https://github.com/Shinmera/random-state.git") 59 | :serial T 60 | :components ((:file "viewer")) 61 | :depends-on (:random-state 62 | :zpng 63 | :trivial-features 64 | :uiop)) 65 | -------------------------------------------------------------------------------- /rc4.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator rc4 8 (stateful-generator) 4 | ((index1 0 :type (unsigned-byte 8)) 5 | (index2 0 :type (unsigned-byte 8)) 6 | (state (make-array 256 :element-type '(unsigned-byte 8)) :type (simple-array (unsigned-byte 8) (256)))) 7 | (:reseed 8 | (loop for i from 0 below 256 9 | do (setf (aref state i) i)) 10 | (setf index1 0) 11 | (setf index2 0) 12 | (let ((seed-length (integer-length seed))) 13 | (loop for i of-type (integer 0 256) from 0 below 256 14 | for j of-type (integer 0 256) = 0 15 | then (mod (+ j 16 | (aref state i) 17 | (ldb (byte 8 (mod (* 8 i) seed-length)) seed)) 18 | 256) 19 | do (rotatef (aref state i) 20 | (aref state j))))) 21 | (:next 22 | (incfmod index1 256) 23 | (incfmod index2 256 (aref state index1)) 24 | (rotatef (aref state index1) 25 | (aref state index2)) 26 | (aref state 27 | (mod 28 | (+ (aref state index1) 29 | (aref state index2)) 30 | 256)))) 31 | -------------------------------------------------------------------------------- /sobol.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | ;;;; Based on 3 | ;;;; https://github.com/stevengj/nlopt/blob/master/src/util/sobolseq.c 4 | ;;;; https://github.com/stevengj/nlopt/blob/master/src/util/soboldata.h 5 | 6 | (define-pregenerated sobol-a 7 | (%arr (unsigned-byte 16) 8 | 3 7 11 13 19 25 37 59 47 61 55 41 67 97 91 109 103 115 131 193 137 145 9 | 143 241 157 185 167 229 171 213 191 253 203 211 239 247 285 369 299 10 | 301 333 351 355 357 361 391 397 425 451 463 487 501 529 539 545 557 11 | 563 601 607 617 623 631 637 647 661 675 677 687 695 701 719 721 731 12 | 757 761 787 789 799 803 817 827 847 859 865 875 877 883 895 901 911 13 | 949 953 967 971 973 981 985 995 1001 1019 1033 1051 1063 1069 1125 14 | 1135 1153 1163 1221 1239 1255 1267 1279 1293 1305 1315 1329 1341 1347 15 | 1367 1387 1413 1423 1431 1441 1479 1509 1527 1531 1555 1557 1573 1591 16 | 1603 1615 1627 1657 1663 1673 1717 1729 1747 1759 1789 1815 1821 1825 17 | 1849 1863 1869 1877 1881 1891 1917 1933 1939 1969 2011 2035 2041 2053 18 | 2071 2091 2093 2119 2147 2149 2161 2171 2189 2197 2207 2217 2225 2255 19 | 2257 2273 2279 2283 2293 2317 2323 2341 2345 2363 2365 2373 2377 2385 20 | 2395 2419 2421 2431 2435 2447 2475 2477 2489 2503 2521 2533 2551 2561 21 | 2567 2579 2581 2601 2633 2657 2669 2681 2687 2693 2705 2717 2727 2731 22 | 2739 2741 2773 2783 2793 2799 2801 2811 2819 2825 2833 2867 2879 2881 23 | 2891 2905 2911 2917 2927 2941 2951 2955 2963 2965 2991 2999 3005 3017 24 | 3035 3037 3047 3053 3083 3085 3097 3103 3159 3169 3179 3187 3205 3209 25 | 3223 3227 3229 3251 3263 3271 3277 3283 3285 3299 3305 3319 3331 3343 26 | 3357 3367 3373 3393 3399 3413 3417 3427 3439 3441 3475 3487 3497 3515 27 | 3517 3529 3543 3547 3553 3559 3573 3589 3613 3617 3623 3627 3635 3641 28 | 3655 3659 3669 3679 3697 3707 3709 3713 3731 3743 3747 3771 3791 3805 29 | 3827 3833 3851 3865 3889 3895 3933 3947 3949 3957 3971 3985 3991 3995 30 | 4007 4013 4021 4045 4051 4069 4073 4179 4201 4219 4221 4249 4305 4331 31 | 4359 4383 4387 4411 4431 4439 4449 4459 4485 4531 4569 4575 4621 4663 32 | 4669 4711 4723 4735 4793 4801 4811 4879 4893 4897 4921 4927 4941 4977 33 | 5017 5027 5033 5127 5169 5175 5199 5213 5223 5237 5287 5293 5331 5391 34 | 5405 5453 5523 5573 5591 5597 5611 5641 5703 5717 5721 5797 5821 5909 35 | 5913 5955 5957 6005 6025 6061 6067 6079 6081 6231 6237 6289 6295 6329 36 | 6383 6427 6453 6465 6501 6523 6539 6577 6589 6601 6607 6631 6683 6699 37 | 6707 6761 6795 6865 6881 6901 6923 6931 6943 6999 7057 7079 7103 7105 38 | 7123 7173 7185 7191 7207 7245 7303 7327 7333 7355 7365 7369 7375 7411 39 | 7431 7459 7491 7505 7515 7541 7557 7561 7701 7705 7727 7749 7761 7783 40 | 7795 7823 7907 7953 7963 7975 8049 8089 8123 8125 8137 8219 8231 8245 41 | 8275 8293 8303 8331 8333 8351 8357 8367 8379 8381 8387 8393 8417 8435 42 | 8461 8469 8489 8495 8507 8515 8551 8555 8569 8585 8599 8605 8639 8641 43 | 8647 8653 8671 8675 8689 8699 8729 8741 8759 8765 8771 8795 8797 8825 44 | 8831 8841 8855 8859 8883 8895 8909 8943 8951 8955 8965 8999 9003 9031 45 | 9045 9049 9071 9073 9085 9095 9101 9109 9123 9129 9137 9143 9147 9185 46 | 9197 9209 9227 9235 9247 9253 9257 9277 9297 9303 9313 9325 9343 9347 47 | 9371 9373 9397 9407 9409 9415 9419 9443 9481 9495 9501 9505 9517 9529 48 | 9555 9557 9571 9585 9591 9607 9611 9621 9625 9631 9647 9661 9669 9679 49 | 9687 9707 9731 9733 9745 9773 9791 9803 9811 9817 9833 9847 9851 9863 50 | 9875 9881 9905 9911 9917 9923 9963 9973 10003 10025 10043 10063 10071 51 | 10077 10091 10099 10105 10115 10129 10145 10169 10183 10187 10207 52 | 10223 10225 10247 10265 10271 10275 10289 10299 10301 10309 10343 53 | 10357 10373 10411 10413 10431 10445 10453 10463 10467 10473 10491 54 | 10505 10511 10513 10523 10539 10549 10559 10561 10571 10581 10615 55 | 10621 10625 10643 10655 10671 10679 10685 10691 10711 10739 10741 56 | 10755 10767 10781 10785 10803 10805 10829 10857 10863 10865 10875 57 | 10877 10917 10921 10929 10949 10967 10971 10987 10995 11009 11029 58 | 11043 11045 11055 11063 11075 11081 11117 11135 11141 11159 11163 59 | 11181 11187 11225 11237 11261 11279 11297 11307 11309 11327 11329 60 | 11341 11377 11403 11405 11413 11427 11439 11453 11461 11473 11479 61 | 11489 11495 11499 11533 11545 11561 11567 11575 11579 11589 11611 62 | 11623 11637 11657 11663 11687 11691 11701 11747 11761 11773 11783 63 | 11795 11797 11817 11849 11855 11867 11869 11873 11883 11919 11921 64 | 11927 11933 11947 11955 11961 11999 12027 12029 12037 12041 12049 65 | 12055 12095 12097 12107 12109 12121 12127 12133 12137 12181 12197 66 | 12207 12209 12239 12253 12263 12269 12277 12287 12295 12309 12313 67 | 12335 12361 12367 12391 12409 12415 12433 12449 12469 12479 12481 68 | 12499 12505 12517 12527 12549 12559 12597 12615 12621 12639 12643 69 | 12657 12667 12707 12713 12727 12741 12745 12763 12769 12779 12781 70 | 12787 12799 12809 12815 12829 12839 12857 12875 12883 12889 12901 71 | 12929 12947 12953 12959 12969 12983 12987 12995 13015 13019 13031 72 | 13063 13077 13103 13137 13149 13173 13207 13211 13227 13241 13249 73 | 13255 13269 13283 13285 13303 13307 13321 13339 13351 13377 13389 74 | 13407 13417 13431 13435 13447 13459 13465 13477 13501 13513 13531 75 | 13543 13561 13581 13599 13605 13617 13623 13637 13647 13661 13677 76 | 13683 13695 13725 13729 13753 13773 13781 13785 13795 13801 13807 77 | 13825 13835 13855 13861 13871 13883 13897 13905 13915 13939 13941 78 | 13969 13979 13981 13997 14027 14035 14037 14051 14063 14085 14095 79 | 14107 14113 14125 14137 14145 14151 14163 14193 14199 14219 14229 80 | 14233 14243 14277 14287 14289 14295 14301 14305 14323 14339 14341 81 | 14359 14365 14375 14387 14411 14425 14441 14449 14499 14513 14523 82 | 14537 14543 14561 14579 14585 14593 14599 14603 14611 14641 14671 83 | 14695 14701 14723 14725 14743 14753 14759 14765 14795 14797 14803 84 | 14831 14839 14845 14855 14889 14895 14909 14929 14941 14945 14951 85 | 14963 14965 14985 15033 15039 15053 15059 15061 15071 15077 15081 86 | 15099 15121 15147 15149 15157 15167 15187 15193 15203 15205 15215 87 | 15217 15223 15243 15257 15269 15273 15287 15291 15313 15335 15347 88 | 15359 15373 15379 15381 15391 15395 15397 15419 15439 15453 15469 89 | 15491 15503 15517 15527 15531 15545 15559 15593 15611 15613 15619 90 | 15639 15643 15649 15661 15667 15669 15681 15693 15717 15721 15741 91 | 15745 15765 15793 15799 15811 15825 15835 15847 15851 15865 15877 92 | 15881 15887 15899 15915 15935 15937 15955 15973 15977 16011 16035 93 | 16061 16069 16087 16093 16097 16121 16141 16153 16159 16165 16183 94 | 16189 16195 16197 16201 16209 16215 16225 16259 16265 16273 16299 95 | 16309 16355 16375 16381)) 96 | 97 | (define-pregenerated sobol-init 98 | (%arr (unsigned-byte 32) 99 | (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 100 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 101 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 102 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 103 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 104 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 105 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 106 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 107 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 108 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 109 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 110 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 111 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 112 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 113 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 114 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 115 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 116 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 117 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 118 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 119 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 120 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 121 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 122 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 123 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 124 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 125 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 126 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 127 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 128 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 129 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 130 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1) 131 | (0 1 3 1 3 1 3 3 1 3 1 3 1 3 1 1 3 1 3 1 3 1 3 3 1 1 1 3 1 3 1 3 3 1 3 132 | 1 1 1 3 1 3 1 1 1 3 3 1 3 3 1 1 3 3 1 3 3 3 1 3 1 3 1 1 3 3 1 1 1 1 3 133 | 1 1 3 1 1 1 3 3 1 3 3 1 3 3 3 1 3 3 3 1 3 3 1 3 3 3 1 3 1 3 1 1 3 3 1 134 | 3 3 1 1 1 3 3 1 3 3 1 3 1 1 3 3 3 1 1 1 3 1 1 3 1 1 3 3 1 3 1 3 3 3 3 135 | 1 1 1 3 3 1 1 3 1 1 1 1 1 1 3 1 3 1 1 1 3 1 3 1 3 3 3 1 1 3 3 1 3 1 3 136 | 1 1 3 1 3 1 3 1 3 1 1 1 3 3 1 3 3 1 3 1 1 1 3 1 3 1 1 3 1 1 3 3 1 1 3 137 | 3 3 1 3 3 3 1 3 1 3 1 1 1 3 1 1 1 3 1 1 1 1 1 3 3 3 1 1 1 1 3 3 3 1 3 138 | 3 1 1 1 1 3 1 1 3 1 3 3 1 1 3 3 1 1 1 1 3 1 3 3 1 3 3 1 1 1 3 3 3 1 3 139 | 3 1 3 3 1 3 1 3 3 3 1 3 1 1 3 1 3 1 1 1 3 3 3 1 1 3 1 3 1 1 1 1 1 1 3 140 | 1 1 3 1 3 3 1 1 1 1 3 1 3 1 3 1 1 1 1 3 3 1 1 1 1 1 3 3 3 1 1 3 3 3 3 141 | 3 1 3 3 1 3 3 3 3 1 1 1 1 1 1 3 1 1 3 1 1 1 3 1 1 1 3 3 3 1 3 1 1 3 3 142 | 3 1 3 3 1 3 1 3 3 1 3 3 3 1 1 3 3 1 3 1 3 1 1 1 3 3 3 3 1 3 1 1 3 1 3 143 | 1 1 1 3 1 3 1 3 1 3 3 3 3 3 3 3 3 1 3 3 3 3 3 1 3 1 3 3 3 1 3 1 3 1 3 144 | 3 1 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 3 3 1 1 3 3 1 1 1 3 3 1 1 3 3 3 3 1 145 | 1 3 1 3 3 1 3 3 1 1 1 3 3 3 1 1 3 3 3 3 3 1 1 1 3 1 3 3 1 3 3 3 3 1 1 146 | 3 1 1 3 1 3 1 3 1 3 3 1 1 3 3 1 3 3 1 3 3 1 1 3 1 3 3 1 1 3 1 3 1 3 1 147 | 1 3 3 1 1 1 3 3 1 3 1 1 3 3 1 1 3 1 3 1 1 1 1 1 3 1 1 1 1 3 1 3 1 1 3 148 | 3 1 1 3 1 3 1 3 3 3 1 3 3 3 1 1 3 3 3 1 1 1 1 3 1 3 1 3 1 1 3 3 1 1 1 149 | 3 3 1 3 1 3 1 1 1 1 1 1 3 1 3 3 1 3 3 3 1 3 1 1 3 3 1 1 3 3 1 1 1 3 1 150 | 3 3 1 1 3 1 1 3 1 3 1 1 1 3 3 3 3 1 1 3 3 1 1 1 1 3 1 1 3 3 3 1 1 3 3 151 | 1 3 3 1 1 3 3 3 3 3 3 3 1 3 3 1 3 1 3 1 1 3 3 1 1 1 3 1 3 3 1 3 3 1 3 152 | 1 1 3 3 3 1 1 1 3 1 1 1 3 3 3 1 3 3 1 3 1 1 3 3 3 1 3 3 1 1 1 3 1 3 3 153 | 3 3 3 3 3 3 1 3 3 1 3 1 1 3 3 3 1 3 3 3 3 3 1 3 3 3 1 1 1 3 3 1 3 3 1 154 | 3 1 3 1 3 1 3 3 3 3 3 3 1 1 3 1 3 1 1 1 1 1 3 1 1 1 3 1 3 1 1 3 3 3 1 155 | 3 1 3 1 1 3 1 3 3 1 3 1 3 3 1 3 3 1 3 3 3 3 3 3 1 3 1 1 3 3 3 1 1 3 3 156 | 3 3 3 3 3 1 3 3 3 3 1 3 1 3 3 3 1 3 1 3 1 1 1 3 3 1 3 1 1 3 3 1 3 1 1 157 | 1 1 3 1 3 1 1 3 1 3 1 3 3 3 3 3 3 1 3 3 3 3 1 3 3 1 3 3 3 3 3 1 1 1 1 158 | 3 3 3 1 3 3 1 1 3 3 1 1 3 3 1 3 1 1 3 1 3 3 3 3 3 1 3 1 1 3 3 3 3 1 3 159 | 1 1 3 3 3 3 3 3 1 1 3 1 3 1 1 3 1 1 1 1 3 3 1 1 3 1 1 1 3 1 3 1 1 3 3 160 | 1 3 1 1 3 3 3 3 3 1 3 1 1 1 3 1 1 1 3 1 1 3 1 3 3 3 3 3 1 1 1 3 3 3 3 161 | 1 3 3 3 3 1 1 3 3 3 1 3 1 1 3 3 1 3 3 1 1 1 1 1 3 1 1 3 3 1 1 1 3 1 1 162 | 3 3 1 3 3 3 3 3 3 3 3 1 1 3 3 1 1 3 1 3 3 3 3 3 1) 163 | (0 0 7 5 1 3 3 7 5 5 7 7 1 3 3 7 5 1 1 5 3 7 1 7 5 1 3 7 7 1 1 1 5 7 7 164 | 5 1 3 3 7 5 5 5 3 3 3 1 1 5 1 1 5 3 3 3 3 1 3 7 5 7 3 7 1 3 3 5 1 3 5 165 | 5 7 7 7 1 1 3 3 1 1 5 1 5 7 5 1 7 5 3 3 1 5 7 1 7 5 1 7 3 1 7 1 7 3 3 166 | 5 7 3 3 5 1 3 3 1 3 5 1 3 3 3 7 1 1 7 3 1 3 7 5 5 7 5 5 3 1 3 3 3 1 3 167 | 3 7 3 3 1 7 5 1 7 7 5 7 5 1 3 1 7 3 7 3 5 7 3 1 3 3 3 1 5 7 3 3 7 7 7 168 | 5 3 1 7 1 3 7 5 3 3 3 7 1 1 3 1 5 7 1 3 5 3 5 3 3 7 5 5 3 3 1 3 7 7 7 169 | 1 5 7 1 3 1 1 7 1 3 1 7 1 5 3 5 3 1 1 5 5 3 3 5 7 1 5 3 7 7 3 5 3 3 1 170 | 7 3 1 3 5 7 1 3 7 1 5 1 3 1 5 3 1 7 1 5 5 5 3 7 1 1 7 3 1 1 7 5 7 5 7 171 | 7 3 7 1 3 7 7 3 5 1 1 7 1 5 5 5 1 5 1 7 5 5 7 1 1 7 1 7 7 1 1 3 3 3 7 172 | 7 5 3 7 3 1 3 7 5 3 3 5 7 1 1 5 5 7 7 1 1 1 1 5 5 5 7 5 7 1 1 3 5 1 3 173 | 3 7 3 7 5 3 5 3 1 7 1 7 7 1 1 7 7 7 5 5 1 1 7 5 5 7 5 1 1 5 5 5 5 5 5 174 | 1 3 1 5 7 3 3 5 7 3 7 1 7 7 1 3 5 1 5 5 3 7 3 7 7 5 7 5 7 1 1 5 3 5 1 175 | 5 3 7 1 5 7 7 3 5 1 3 5 1 5 3 3 3 7 3 5 1 3 7 7 3 7 5 3 3 1 7 5 1 1 3 176 | 7 1 7 1 7 3 7 3 5 7 3 5 3 1 1 1 5 7 7 3 3 1 1 1 5 5 7 3 1 1 3 3 7 3 3 177 | 5 1 3 7 3 3 7 3 5 7 5 7 7 3 3 5 1 3 5 3 1 3 5 1 1 3 7 7 1 5 1 3 7 3 7 178 | 3 5 1 7 1 1 3 5 3 7 1 5 5 1 1 3 1 3 3 7 1 7 3 1 7 3 1 7 3 5 3 5 7 3 3 179 | 3 5 1 7 7 1 3 1 3 7 7 1 3 7 3 1 5 3 1 1 1 5 3 3 7 1 5 3 5 1 3 1 3 1 5 180 | 7 7 1 1 5 3 1 5 1 1 7 7 3 5 5 1 7 1 5 1 1 3 1 5 7 5 7 7 1 5 1 1 3 5 1 181 | 5 5 3 1 3 1 5 5 3 3 3 3 1 1 3 1 3 5 5 7 5 5 7 5 7 1 3 7 7 3 5 5 7 5 5 182 | 3 3 3 1 7 1 5 5 5 3 3 5 1 3 1 3 3 3 7 1 7 7 3 7 1 1 5 7 1 7 1 7 7 1 3 183 | 7 5 1 3 5 5 5 1 1 7 1 7 1 7 7 3 1 1 5 1 5 1 5 3 5 5 5 5 5 3 3 7 3 3 5 184 | 5 3 7 1 5 7 5 1 5 5 3 5 5 7 5 3 5 5 5 1 5 5 5 5 1 3 5 3 1 7 5 5 7 1 5 185 | 3 3 1 5 3 7 1 7 5 1 1 3 1 1 7 1 5 5 3 7 3 7 5 3 1 1 3 1 3 5 5 7 5 3 7 186 | 7 7 3 7 3 7 1 3 1 7 7 1 7 3 7 3 7 3 7 3 5 1 1 7 3 1 5 5 7 1 5 5 5 7 1 187 | 5 5 1 5 5 3 1 3 1 7 3 1 3 5 7 7 7 1 1 7 3 1 5 5 5 1 1 1 1 1 5 3 5 1 3 188 | 5 3 1 1 1 1 3 7 3 7 5 7 1 5 5 7 5 3 3 7 5 3 1 1 3 1 3 1 1 3 7 1 7 1 1 189 | 5 1 7 5 3 7 3 5 3 1 1 5 5 1 7 7 3 7 3 7 1 5 1 5 3 7 3 5 7 7 7 3 3 1 1 190 | 5 5 3 7 1 1 1 3 5 3 1 1 3 3 7 5 1 1 3 7 1 5 7 3 7 5 5 7 3 5 3 1 5 3 1 191 | 1 7 5 1 7 3 7 5 1 7 1 7 7 1 1 7 1 5 5 1 1 7 5 7 1 5 3 5 3 3 7 1 5 1 1 192 | 5 5 3 3 7 5 5 1 1 1 3 1 5 7 7 1 7 5 7 3 7 3 1 3 7 3 1 5 5 3 5 1 3 5 5 193 | 5 1 1 7 7 1 5 5 1 3 5 1 5 3 5 3 3 7 5 7 3 7 3 1 3 7 7 3 3 1 1 3 3 3 3 194 | 3 5 5 3 3 3 1 3 5 7 7 1 5 7 3 7 1 1 3 5 7 5 3 3 3) 195 | (0 0 0 0 1 7 9 13 11 1 3 7 9 5 13 13 11 3 15 5 3 15 7 9 13 9 1 11 7 5 196 | 15 1 15 11 5 11 1 7 9 7 7 1 15 15 15 13 3 3 15 5 9 7 13 3 7 5 11 9 1 9 197 | 1 5 7 13 9 9 1 7 3 5 1 11 11 13 7 7 9 9 1 1 3 9 15 1 5 13 1 9 9 9 9 9 198 | 13 11 3 5 11 11 13 5 3 15 1 11 11 7 13 15 11 13 9 11 15 15 13 3 15 7 9 199 | 11 13 11 9 9 5 13 9 1 13 7 7 7 7 7 5 9 7 13 11 9 11 15 3 13 11 1 11 3 200 | 3 9 11 1 7 1 15 15 3 1 9 1 7 13 11 3 13 11 7 3 3 5 13 11 5 11 1 3 9 7 201 | 15 7 5 13 7 9 13 15 13 9 7 15 7 9 5 11 11 13 13 9 3 5 13 9 11 15 11 7 202 | 1 7 13 3 13 3 13 9 15 7 13 13 3 13 15 15 11 9 13 9 15 1 1 15 11 11 7 1 203 | 11 13 9 13 3 5 11 13 9 9 13 1 11 15 13 3 13 7 15 1 15 3 3 11 7 13 7 7 204 | 9 7 5 15 9 5 5 7 15 13 15 5 15 5 3 1 11 7 1 5 7 9 3 11 1 15 1 3 15 11 205 | 13 5 13 1 7 1 15 7 5 1 1 15 13 11 11 13 5 11 7 9 7 1 5 3 9 5 5 11 5 1 206 | 7 1 11 7 9 13 15 13 3 1 11 13 15 1 1 11 9 13 3 13 11 15 13 9 9 9 5 5 5 207 | 5 1 15 5 9 11 7 15 5 3 13 5 3 11 5 1 11 13 9 11 3 7 13 15 1 7 11 1 13 208 | 1 15 1 9 7 3 9 11 1 9 13 13 3 11 7 9 1 7 15 9 1 5 13 5 11 3 9 15 11 13 209 | 5 1 7 7 5 13 7 7 9 5 11 11 1 1 15 3 13 9 13 9 9 11 5 5 13 15 3 9 15 3 210 | 11 11 15 15 3 11 15 15 3 1 3 1 3 3 1 3 13 1 11 5 15 7 15 9 1 7 1 9 11 211 | 15 1 13 9 13 11 7 3 7 3 13 7 9 7 7 3 3 9 9 7 5 11 13 13 7 7 15 9 5 5 3 212 | 3 13 3 9 3 1 11 1 3 11 15 11 11 11 9 13 7 9 15 9 11 1 3 3 9 7 15 13 13 213 | 7 15 9 13 9 15 13 15 9 13 1 11 7 11 3 13 5 1 7 15 3 13 7 13 13 11 3 5 214 | 3 13 11 9 9 3 11 11 7 9 13 11 7 15 13 7 5 3 1 5 15 15 3 11 1 7 3 15 11 215 | 5 5 3 5 5 1 15 5 1 5 3 7 5 11 3 13 9 13 15 5 3 5 9 5 3 11 1 13 9 15 3 216 | 5 11 9 1 3 15 9 9 9 11 7 5 13 1 15 3 13 9 13 5 1 5 1 13 13 7 7 1 9 5 217 | 11 9 11 13 3 15 15 13 15 7 5 7 9 7 9 9 9 11 9 3 11 15 13 13 5 9 15 1 1 218 | 9 5 13 3 13 15 3 1 3 11 13 1 15 9 9 3 1 9 1 9 1 13 11 15 7 11 15 13 15 219 | 1 9 9 7 3 5 11 7 3 9 5 15 7 5 3 13 7 1 1 9 15 15 15 11 3 5 15 13 7 15 220 | 15 11 11 9 5 15 9 7 3 13 1 1 5 1 3 1 7 1 1 5 1 11 11 9 9 5 13 7 7 7 1 221 | 1 9 9 11 11 15 7 5 5 3 11 1 3 7 13 7 7 7 3 15 15 11 9 3 9 3 15 13 5 3 222 | 3 3 5 9 15 9 9 1 5 9 9 15 5 15 7 9 1 9 9 5 11 5 15 15 11 7 7 7 1 1 11 223 | 11 13 15 3 13 5 1 7 1 11 3 13 15 3 5 3 5 7 3 9 9 5 1 7 11 9 3 5 11 13 224 | 13 13 9 15 5 7 1 15 11 9 15 15 13 13 13 1 11 9 15 9 5 15 5 7 3 11 3 15 225 | 7 13 11 7 3 7 13 5 13 15 5 13 9 1 15 11 5 5 1 11 3 3 7 1 9 7 15 9 9 3 226 | 11 15 7 1 3 1 1 1 9 1 5 15 15 7 5 5 7 9 7 15 13 13 11 1 9 11 1 13 1 7 227 | 15 15 5 5 1 11 3 9 11 9 9 9 1 9 3 5 15 1 1 9 7 3 3 1 9 9 11 9 9 13 13 228 | 3 13 11 13 5 1 5 5 9 9 3 13 13 9 15 9 11 7 11 9 13 9 1 15 9 7 7 1 7 9 229 | 9 15 1 11 1 13 13 15 9 13 7 15 3 9 3 1 13 7 5 9 3 1 7 1 1 13 3 3 11 1 230 | 7 13 15 15 5 7 13 13 15 11 13 1 13 13 3 9 15 15 11 15 9 15 1 13 15 1 1 231 | 5 11 5 1 11 11 5 3 9 1 3 5 13 9 7 7 1 9 9 15 7 5 5 15 13 9 7 13 3 13 232 | 11 13 7 9 13 13 13 15 9 5 5 3 3 3 1 3 15) 233 | (0 0 0 0 0 0 9 3 27 15 29 21 23 19 11 25 7 13 17 1 25 29 3 31 11 5 23 234 | 27 19 21 5 1 17 13 7 15 9 31 25 3 5 23 7 3 17 23 3 3 21 25 25 23 11 19 235 | 3 11 31 7 9 5 17 23 17 17 25 13 11 31 27 19 17 23 7 5 11 19 19 7 13 21 236 | 21 7 9 11 1 5 21 11 13 25 9 7 7 27 15 25 15 21 17 19 19 21 5 11 3 5 29 237 | 31 29 5 5 1 31 27 11 13 1 3 7 11 7 3 23 13 31 17 1 27 11 25 1 23 29 17 238 | 25 7 25 27 17 13 17 23 5 17 5 13 11 21 5 11 5 9 31 19 17 9 9 27 21 15 239 | 15 1 1 29 5 31 11 17 23 19 21 25 15 11 5 5 1 19 19 19 7 13 21 17 17 25 240 | 23 19 23 15 13 5 19 25 9 7 3 21 17 25 1 27 25 27 25 9 13 3 17 25 23 9 241 | 25 9 13 17 17 3 15 7 7 29 3 19 29 29 19 29 13 15 25 27 1 3 9 9 13 31 242 | 29 31 5 15 29 1 19 5 9 19 5 15 3 5 7 15 17 17 23 11 9 23 19 3 17 1 27 243 | 9 9 17 13 25 29 23 29 11 31 25 21 29 19 27 31 3 5 3 3 13 21 9 29 3 17 244 | 11 11 9 21 19 7 17 31 25 1 27 5 15 27 29 29 29 25 27 25 3 21 17 25 13 245 | 15 17 13 23 9 3 11 7 9 9 7 17 7 1 27 1 9 5 31 21 25 25 21 11 1 23 19 246 | 27 15 3 5 23 9 25 7 29 11 9 13 5 11 1 3 31 27 3 17 27 11 13 15 29 15 1 247 | 15 23 25 13 21 15 3 29 29 5 25 17 11 7 15 5 21 7 31 13 11 23 5 7 23 27 248 | 21 29 15 7 27 27 19 7 15 27 27 19 19 9 15 1 3 29 29 5 27 31 9 1 7 3 19 249 | 19 29 9 3 21 31 29 25 1 3 9 27 5 27 25 21 11 29 31 27 21 29 17 9 17 13 250 | 11 25 15 21 11 19 31 3 19 5 3 3 9 13 13 3 29 7 5 9 23 13 21 23 21 31 251 | 11 7 7 3 23 1 23 5 9 17 21 1 17 29 7 5 17 13 25 17 9 19 9 5 7 21 19 13 252 | 9 7 3 9 3 15 31 29 29 25 13 9 21 9 31 7 15 5 31 7 15 27 25 19 9 9 25 253 | 25 23 1 9 7 11 15 19 15 27 17 11 11 31 13 25 25 9 7 13 29 19 5 19 31 254 | 25 13 25 15 5 9 29 31 9 29 27 25 27 11 17 5 17 3 23 15 9 9 17 17 31 11 255 | 19 25 13 23 15 25 21 31 19 3 11 25 7 15 19 7 5 3 13 13 1 23 5 25 11 25 256 | 15 13 21 11 23 29 5 17 27 9 19 15 5 29 23 19 1 27 3 23 21 19 27 11 17 257 | 13 27 11 31 23 5 9 21 31 29 11 21 17 15 7 15 7 9 21 27 25 29 11 3 21 258 | 13 23 19 27 17 29 25 17 9 1 19 23 5 23 1 17 17 13 27 23 7 7 11 13 17 259 | 13 11 21 13 23 1 27 13 9 7 1 27 29 5 13 25 21 3 31 15 13 3 19 13 1 27 260 | 15 17 1 3 13 13 13 31 29 27 7 7 21 29 15 17 17 21 19 17 3 15 5 27 27 3 261 | 31 31 7 21 3 13 11 17 27 25 1 9 7 29 27 21 23 13 25 29 15 17 29 9 15 3 262 | 21 15 17 17 31 9 9 23 19 25 3 1 11 27 29 1 31 29 25 29 1 23 29 25 13 3 263 | 31 25 5 5 11 3 21 9 23 7 11 23 11 1 1 3 23 25 23 1 23 3 27 9 27 3 23 264 | 25 19 29 29 13 27 5 9 29 29 13 17 3 23 19 7 13 3 19 23 5 29 29 13 13 5 265 | 19 5 17 9 11 11 29 27 23 19 17 25 13 1 13 3 11 1 17 29 1 13 17 9 17 21 266 | 1 11 1 1 25 5 7 29 29 19 19 1 29 13 3 1 31 15 13 3 1 11 19 5 29 13 29 267 | 23 3 1 31 13 19 17 5 5 1 29 23 3 19 25 19 27 9 27 13 15 29 23 13 25 25 268 | 17 19 17 15 27 3 25 17 27 3 27 31 23 13 31 11 15 7 21 19 27 19 21 29 7 269 | 31 13 9 9 7 21 13 11 9 11 29 19 11 19 21 5 29 13 7 19 19 27 23 31 1 27 270 | 21 7 3 7 11 23 13 29 11 31 19 1 5 5 11 5 3 27 5 7 11 31 1 27 31 31 23 271 | 5 21 27 9 25 3 15 19 1 19 9 5 25 21 15 25 29 15 21 11 19 15 3 7 13 11 272 | 25 17 1 5 31 13 29 23 9 5 29 7 17 27 7 17 31 9 31 9 9 7 21 3 3 3 9 11 273 | 21 11 31 9 25 5 1 31 13 29 9 29 1 11 19 7 27 13 31 7 31 7 25 23 21 29 274 | 11 11 13 11 27 1 23 31 21 23 21 19 31 5 31 25 25 19 17 11 25 7 13 1 29 275 | 17 23 15 7 29 17 13 3 17) 276 | (0 0 0 0 0 0 0 0 0 0 0 0 37 33 7 5 11 39 63 59 17 15 23 29 3 21 13 31 277 | 25 9 49 33 19 29 11 19 27 15 25 63 55 17 63 49 19 41 59 3 57 33 49 53 278 | 57 57 39 21 7 53 9 55 15 59 19 49 31 3 39 5 5 41 9 19 9 57 25 1 15 51 279 | 11 19 61 53 29 19 11 9 21 19 43 13 13 41 25 31 9 11 19 5 53 37 7 51 45 280 | 7 7 61 23 45 7 59 41 1 29 61 37 27 47 15 31 35 31 17 51 13 25 45 5 5 281 | 33 39 5 47 29 35 47 63 45 37 47 59 21 59 33 51 9 27 13 25 43 3 17 21 282 | 59 61 27 47 57 11 17 39 1 63 21 59 17 13 31 3 31 7 9 27 37 23 31 9 45 283 | 43 31 63 21 39 51 27 7 53 11 1 59 39 23 49 23 7 55 59 3 19 35 13 9 13 284 | 15 23 9 7 43 55 3 19 9 27 33 27 49 23 47 19 7 11 55 27 35 5 5 55 35 37 285 | 9 33 29 47 25 11 47 53 61 59 3 53 47 5 19 59 5 47 23 45 53 3 49 61 47 286 | 39 29 17 57 5 17 31 23 41 39 5 27 7 29 29 33 31 41 31 29 17 29 29 9 9 287 | 31 27 53 35 5 61 1 49 13 57 29 5 21 43 25 57 49 37 27 11 61 37 49 5 63 288 | 63 3 45 37 63 21 21 19 27 59 21 45 23 13 15 3 43 63 39 19 63 31 41 41 289 | 15 43 63 53 1 63 31 7 17 11 61 31 51 37 29 59 25 63 59 47 15 27 19 29 290 | 45 35 55 39 19 43 21 19 13 17 51 37 5 33 35 49 25 45 1 63 47 9 63 15 291 | 25 25 15 41 13 3 19 51 49 37 25 49 13 53 47 23 35 29 33 21 35 23 3 43 292 | 31 63 9 1 61 43 3 11 55 11 35 1 63 35 49 19 45 9 57 51 1 47 41 9 11 37 293 | 19 55 23 55 55 13 7 47 37 11 43 17 3 25 19 55 59 37 33 43 1 5 21 5 63 294 | 49 61 21 51 15 19 43 47 17 9 53 45 11 51 25 11 25 47 47 1 43 29 17 31 295 | 15 59 27 63 11 41 51 29 7 27 63 31 43 3 29 39 3 59 59 1 53 63 23 63 47 296 | 51 23 61 39 47 21 39 15 3 9 57 61 39 37 21 51 1 23 43 27 25 11 13 21 297 | 43 7 11 33 55 1 37 35 27 61 39 5 19 61 61 57 59 21 59 61 57 25 55 27 298 | 31 41 33 63 19 57 35 13 63 35 17 11 11 49 41 55 5 45 17 35 5 31 31 37 299 | 17 45 51 1 39 49 55 19 41 13 5 51 5 49 1 21 13 17 59 51 11 3 61 1 33 300 | 37 33 61 25 27 59 7 49 13 63 3 33 3 15 9 13 35 39 11 59 59 1 57 11 5 301 | 57 13 31 13 11 55 45 9 55 55 19 25 41 23 45 29 63 59 27 39 21 37 7 61 302 | 49 35 39 9 29 7 25 23 57 5 19 15 33 49 37 25 17 45 29 15 25 3 3 49 11 303 | 39 15 19 57 39 15 11 3 57 31 55 61 19 5 41 35 59 61 39 41 53 53 63 31 304 | 9 59 13 35 55 41 49 5 41 25 27 43 5 5 43 5 5 17 5 15 27 29 17 9 3 55 305 | 31 1 45 45 13 57 17 3 61 15 49 15 47 9 37 45 9 51 61 21 33 11 21 63 63 306 | 47 57 61 49 9 59 19 29 21 23 55 23 43 41 57 9 39 27 41 35 61 29 57 63 307 | 21 31 59 35 49 3 49 47 49 33 21 19 21 35 11 17 37 23 59 13 37 35 55 57 308 | 1 29 45 11 1 15 9 33 19 53 43 39 23 7 13 13 1 19 41 55 1 13 15 59 55 309 | 15 3 57 37 31 17 1 3 21 29 25 55 9 37 33 53 41 51 19 57 13 63 43 19 7 310 | 13 37 33 19 15 63 51 11 49 23 57 47 51 15 53 41 1 15 37 61 11 35 29 33 311 | 23 55 11 59 19 61 61 45 13 49 13 63 5 61 5 31 17 61 63 13 27 57 1 21 5 312 | 11 39 57 51 53 39 25 41 39 37 23 31 25 33 17 57 29 27 23 47 41 29 19 313 | 47 41 25 5 51 43 39 29 7 31 45 51 49 55 17 43 49 45 9 29 3 5 47 9 15 314 | 19 51 45 57 63 9 21 59 3 9 13 45 23 15 31 21 15 51 35 9 11 61 23 53 29 315 | 51 45 31 29 5 35 29 53 35 17 59 55 27 51 59 27 47 15 29 37 7 49 55 5 316 | 19 45 29 19 57 33 53 45 21 9 3 35 29 43 31 39 3 45 1 41 29 5 59 41 33 317 | 35 27 19 13 25 27 43 33 35 17 17 23 7 35 15 61 61 53 5 15 23 11 13 43 318 | 55 47 25 43 15 57 45 1 49 63 57 15 31 31 7 53 27 15 47 23 7 29 53 47 9 319 | 53 3 25 55 45 63 21 17 23 31 27 27 43 63 55 63 45 51 15 27 5 37 43 11 320 | 27 5 27 59 21 7 39 27 63 35 47 55 17 17 17 3 19 21 13 49 61 39 15) 321 | (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 33 115 41 79 17 29 119 75 73 322 | 105 7 59 65 21 3 113 61 89 45 107 21 71 79 19 71 61 41 57 121 87 119 323 | 55 85 121 119 11 23 61 11 35 33 43 107 113 101 29 87 119 97 29 17 89 5 324 | 127 89 119 117 103 105 41 83 25 41 55 69 117 49 127 29 1 99 53 83 15 325 | 31 73 115 35 21 89 5 1 91 53 35 95 83 19 85 55 51 101 33 41 55 45 95 326 | 61 27 37 89 75 57 61 15 117 15 21 27 25 27 123 39 109 93 51 21 91 109 327 | 107 45 15 93 127 3 53 81 79 107 79 87 35 109 73 35 83 107 1 51 7 59 33 328 | 115 43 111 45 121 105 125 87 101 41 95 75 1 57 117 21 27 67 29 53 117 329 | 63 1 77 89 115 49 127 15 79 81 29 65 103 33 73 79 29 21 113 31 33 107 330 | 95 111 59 99 117 63 63 99 39 9 35 63 125 99 45 93 33 93 9 105 75 51 331 | 115 11 37 17 41 21 43 73 19 93 7 95 81 93 79 81 55 9 51 63 45 89 73 19 332 | 115 39 47 81 39 5 5 45 53 65 49 17 105 13 107 5 5 19 73 59 43 83 97 333 | 115 27 1 69 103 3 99 103 63 67 25 121 97 77 13 83 103 41 11 27 81 37 334 | 33 125 71 41 41 59 41 87 123 43 101 63 45 39 21 97 15 97 111 21 49 13 335 | 17 79 91 65 105 75 1 45 67 83 107 125 87 15 81 95 105 65 45 59 103 23 336 | 103 99 67 99 47 117 71 89 35 53 73 9 115 49 37 1 35 9 45 81 19 127 17 337 | 17 105 89 49 101 7 37 33 11 95 95 17 111 105 41 115 5 69 101 27 27 101 338 | 103 53 9 21 43 79 91 65 117 87 125 55 45 63 85 83 97 45 83 87 113 93 339 | 95 5 17 77 77 127 123 45 81 85 121 119 27 85 41 49 15 107 21 51 119 11 340 | 87 101 115 63 63 37 121 109 7 43 69 19 77 49 71 59 35 7 13 55 101 127 341 | 103 85 109 29 61 67 21 111 67 23 57 75 71 101 123 41 107 101 107 125 342 | 27 47 119 41 19 127 33 31 109 7 91 91 39 125 105 47 125 123 91 9 103 343 | 45 23 117 9 125 73 11 37 61 79 21 5 47 117 67 53 85 33 81 121 47 61 51 344 | 127 29 65 45 41 95 57 73 33 117 61 111 59 123 65 47 105 23 29 107 37 345 | 81 67 29 115 119 75 73 99 103 7 57 45 61 95 49 101 101 35 47 119 39 67 346 | 31 103 7 61 127 87 3 35 29 73 95 103 71 75 51 87 57 97 11 105 87 41 73 347 | 109 69 35 121 39 111 1 77 39 47 53 91 3 17 51 83 39 125 85 111 21 69 348 | 85 29 55 11 117 1 47 17 65 63 47 117 17 115 51 25 33 123 123 83 51 113 349 | 95 121 51 91 109 43 55 35 55 87 33 37 5 3 45 21 105 127 35 17 35 37 97 350 | 97 21 77 123 17 89 53 105 75 25 125 13 47 21 125 23 55 63 61 5 17 93 351 | 57 121 69 73 93 121 105 75 91 67 95 75 9 69 97 99 93 11 53 19 73 5 33 352 | 79 107 65 69 79 125 25 93 55 61 17 117 69 97 87 111 37 93 59 79 95 53 353 | 115 53 85 85 65 59 23 75 21 67 27 99 79 27 3 95 27 69 19 75 47 59 41 354 | 85 77 99 55 49 93 93 119 51 125 63 13 15 45 61 19 105 115 17 83 7 7 11 355 | 61 37 63 89 95 119 113 67 123 91 33 37 99 43 11 33 65 81 79 81 107 63 356 | 63 55 89 91 25 93 101 27 55 75 121 79 43 125 73 27 109 35 21 71 113 89 357 | 59 95 41 45 113 119 113 39 59 73 15 13 59 67 121 27 7 105 15 59 59 35 358 | 91 89 23 125 97 53 41 91 111 29 31 3 103 61 71 35 7 119 29 45 49 111 359 | 41 109 59 125 13 27 19 79 9 75 83 81 33 91 109 33 29 107 111 101 107 360 | 109 65 59 43 37 1 9 15 109 37 111 113 119 79 73 65 71 93 17 101 87 97 361 | 43 23 75 109 41 49 53 31 97 105 109 119 51 9 53 113 97 73 89 79 49 61 362 | 105 13 99 53 71 7 87 21 101 5 71 31 123 121 121 73 79 115 13 39 101 19 363 | 37 51 83 97 55 81 91 127 105 89 63 47 49 75 37 77 15 49 107 23 23 35 364 | 19 69 17 59 63 73 29 125 61 65 95 101 81 57 69 83 37 11 37 95 1 73 27 365 | 29 57 7 65 83 99 69 19 103 43 95 25 19 103 41 125 97 71 105 83 83 61 366 | 39 9 45 117 63 31 5 117 67 125 41 117 43 77 97 15 29 5 59 25 63 87 39 367 | 39 77 85 37 81 73 89 29 125 109 21 23 119 105 43 93 97 15 125 29 51 69 368 | 37 45 31 75 109 119 53 5 101 125 121 35 29 7 63 17 63 13 69 15 105 51 369 | 127 105 9 57 95 59 109 35 49 23 33 107 55 33 57 79 73 69 59 107 55 11 370 | 63 95 103 23 125 91 31 91 51 65 61 75 69 107 65 101 59 35 15) 371 | (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 372 | 0 7 23 39 217 141 27 53 181 169 35 15 207 45 247 185 117 41 81 223 151 373 | 81 189 61 95 185 23 73 113 239 85 9 201 83 53 183 203 91 149 101 13 374 | 111 239 3 205 253 247 121 189 169 179 197 175 217 249 195 95 63 19 7 5 375 | 75 217 245 111 189 165 169 141 221 249 159 253 207 249 219 23 49 127 376 | 237 5 25 177 37 103 65 167 81 87 119 45 79 143 57 79 187 143 183 75 97 377 | 211 149 175 37 135 189 225 241 63 33 43 13 73 213 57 239 183 117 21 29 378 | 115 43 205 223 15 3 159 51 101 127 99 239 171 113 171 119 189 245 201 379 | 27 185 229 105 153 189 33 35 137 77 97 17 181 55 197 201 155 37 197 380 | 137 223 25 179 91 23 235 53 253 49 181 249 53 173 97 247 67 115 103 381 | 159 239 69 173 217 95 221 247 97 91 123 223 213 129 181 87 239 85 89 382 | 249 141 39 57 249 71 101 159 33 137 189 71 253 205 171 13 249 109 131 383 | 199 189 179 31 99 113 41 173 23 189 197 3 135 9 95 195 27 183 1 123 73 384 | 53 99 197 59 27 101 55 193 31 61 119 11 7 255 233 53 157 193 97 83 65 385 | 81 239 167 69 71 109 97 137 71 193 189 115 79 205 37 227 53 33 91 229 386 | 245 105 77 229 161 103 93 13 161 229 223 69 15 25 23 233 93 25 217 247 387 | 61 75 27 9 223 213 55 197 145 89 199 41 201 5 149 35 119 183 53 11 13 388 | 3 179 229 43 55 187 233 47 133 91 47 71 93 105 145 45 255 221 115 175 389 | 19 129 5 209 197 57 177 115 187 119 77 211 111 33 113 23 87 137 41 7 390 | 83 43 121 145 5 219 27 11 111 207 55 97 63 229 53 33 149 23 187 153 91 391 | 193 183 59 211 93 139 59 179 163 209 77 39 111 79 229 85 237 199 137 392 | 147 25 73 121 129 83 87 93 205 167 53 107 229 213 95 219 109 175 13 393 | 209 97 61 147 19 13 123 73 35 141 81 19 171 255 111 107 233 113 133 89 394 | 9 231 95 69 33 1 253 219 253 247 129 11 251 221 153 35 103 239 7 27 395 | 235 181 5 207 53 149 155 225 165 137 155 201 97 245 203 47 39 35 105 396 | 239 49 15 253 7 237 213 55 87 199 27 175 49 41 229 85 3 149 179 129 397 | 185 249 197 15 97 197 139 203 63 33 251 217 199 199 99 249 33 229 177 398 | 13 209 147 97 31 125 177 137 187 11 91 223 29 169 231 59 31 163 41 57 399 | 87 247 25 127 101 207 187 73 61 105 27 91 171 243 33 3 1 21 229 93 71 400 | 61 37 183 65 211 53 11 151 165 47 5 129 79 101 147 169 181 19 95 77 401 | 139 197 219 97 239 183 143 9 13 209 23 215 53 137 203 19 151 171 133 402 | 219 231 3 15 253 225 33 111 183 213 169 119 111 15 201 123 121 225 113 403 | 113 225 161 165 1 139 55 3 93 217 193 97 29 69 231 161 93 69 143 137 9 404 | 87 183 113 183 73 215 137 89 251 163 41 227 145 57 81 57 11 135 145 405 | 161 175 159 25 55 167 157 211 97 247 249 23 129 159 71 197 127 141 219 406 | 5 233 131 217 101 131 33 157 173 69 207 239 81 205 11 41 169 65 193 77 407 | 201 173 1 221 157 1 15 113 147 137 205 225 73 45 49 149 113 253 99 17 408 | 119 105 117 129 243 75 203 53 29 247 35 247 171 31 199 213 29 251 7 409 | 251 187 91 11 149 13 205 37 249 137 139 9 7 113 183 205 187 39 3 79 410 | 155 227 89 185 51 127 63 83 41 133 183 181 127 19 255 219 59 251 3 187 411 | 57 217 115 217 229 181 185 149 83 115 11 123 19 109 165 103 123 219 412 | 129 155 207 177 9 49 181 231 33 233 67 155 41 9 95 123 65 117 249 85 413 | 169 129 241 173 251 225 147 165 69 81 239 95 23 83 227 249 143 171 193 414 | 9 21 57 73 97 57 29 239 151 159 191 47 51 1 223 251 251 151 41 119 127 415 | 131 33 209 123 53 241 25 31 183 107 25 115 39 11 213 239 219 109 185 416 | 35 133 123 185 27 55 245 61 75 205 213 169 163 63 55 49 83 195 51 31 417 | 41 15 203 41 63 127 161 5 143 7 199 251 95 75 101 15 43 237 197 117 418 | 167 155 21 83 205 255 49 101 213 237 135 135 21 73 93 115 7 85 223 237 419 | 79 89 5 57 239 67 65 201 155 71 85 195 89 181 119 135 147 237 173 41 420 | 155 67 113 111 21 183 23 103 207 253 69 219 205 195 43 197 229 139 177 421 | 129 69 97 201 163 189 11 99 91 253 239 91 145 19 179 231 121 7 225 237 422 | 125 191 119 59 175 237 131 79 43 45 205 199 251 153 207 37 179 113 255 423 | 107 217 61 7 181 247 31 13 113 145 107 233 233 43 79 23 169 137 129 424 | 183 53 91 55 103 223 87 177 157 79 213 139 183 231 205 143 129 243 205 425 | 93 59 15 89 9 11 47 133 227 75 9 91 19 171 163 79 7 103 5 119 155 75 426 | 11 71 95 17 13 243 207 187) 427 | (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 428 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 235 307 495 417 57 151 19 119 375 429 | 451 55 449 501 53 185 317 17 21 487 13 347 393 15 391 307 189 381 71 430 | 163 99 467 167 433 337 257 179 47 385 23 117 369 425 207 433 301 147 431 | 333 85 221 423 49 3 43 229 227 201 383 281 229 207 21 343 251 397 173 432 | 507 421 443 399 53 345 77 385 317 155 187 269 501 19 169 235 415 61 433 | 247 183 5 257 401 451 95 455 49 489 75 459 377 87 463 155 233 115 429 434 | 211 419 143 487 195 209 461 193 157 193 363 181 271 445 381 231 135 435 | 327 403 171 197 181 343 113 313 393 311 415 267 247 425 233 289 55 39 436 | 247 327 141 5 189 183 27 337 341 327 87 429 357 265 251 437 201 29 339 437 | 257 377 17 53 327 47 375 393 369 403 125 429 257 157 217 85 267 117 438 | 337 447 219 501 41 41 193 509 131 207 505 421 149 111 177 167 223 291 439 | 91 29 305 151 177 337 183 361 435 307 507 77 181 507 315 145 423 71 440 | 103 493 271 469 339 237 437 483 31 219 61 131 391 233 219 69 57 459 441 | 225 421 7 461 111 451 277 185 193 125 251 199 73 71 7 409 417 149 193 442 | 53 437 29 467 229 31 35 75 105 503 75 317 401 367 131 365 441 433 93 443 | 377 405 465 259 283 443 143 445 3 461 329 309 77 323 155 347 45 381 444 | 315 463 207 321 157 109 479 313 345 167 439 307 235 473 79 101 245 19 445 | 381 251 35 25 107 187 115 113 321 115 445 61 77 293 405 13 53 17 171 446 | 299 41 79 3 485 331 13 257 59 201 497 81 451 199 171 81 253 365 75 451 447 | 149 483 81 453 469 485 305 163 401 15 91 3 129 35 239 355 211 387 101 448 | 299 67 375 405 357 267 363 79 83 437 457 39 97 473 289 179 57 23 49 79 449 | 71 341 287 95 229 271 475 49 241 261 495 353 381 13 291 37 251 105 399 450 | 81 89 265 507 205 145 331 129 119 503 249 1 289 463 163 443 63 123 361 451 | 261 49 429 137 355 175 507 59 277 391 25 185 381 197 39 5 429 119 247 452 | 177 329 465 421 271 467 151 45 429 137 471 11 17 409 347 199 463 177 453 | 11 51 361 95 497 163 351 127 395 511 327 353 49 105 151 321 331 329 454 | 509 107 109 303 467 287 161 45 385 289 363 331 265 407 37 433 315 343 455 | 63 51 185 71 27 267 503 239 293 245 281 297 75 461 371 129 189 189 339 456 | 287 111 111 379 93 27 185 347 337 247 507 161 231 43 499 73 327 263 457 | 331 249 493 37 25 115 3 167 197 127 357 497 103 125 191 165 55 101 95 458 | 79 351 341 43 125 135 173 289 373 133 421 241 281 213 177 363 151 227 459 | 145 363 239 431 81 397 241 67 291 255 405 421 399 75 399 105 329 41 460 | 425 7 283 375 475 427 277 209 411 3 137 195 289 509 121 55 147 275 251 461 | 19 129 285 415 487 491 193 219 403 23 97 65 285 75 21 373 261 339 239 462 | 495 415 333 107 435 297 213 149 463 199 323 45 19 301 121 499 187 229 463 | 63 425 99 281 35 125 349 87 101 59 195 511 355 73 263 243 101 165 141 464 | 11 389 219 187 449 447 393 477 305 221 51 355 209 499 479 265 377 145 465 | 411 173 11 433 483 135 385 341 89 209 391 33 395 319 451 119 341 227 466 | 375 61 331 493 411 293 47 203 375 167 395 155 5 237 361 489 127 21 345 467 | 101 371 233 431 109 119 277 125 263 73 135 123 83 123 405 69 75 287 468 | 401 23 283 393 41 379 431 11 475 505 19 365 265 271 499 489 443 165 91 469 | 83 291 319 199 107 245 389 143 137 89 125 281 381 215 131 299 249 375 470 | 455 43 73 281 217 297 229 431 357 81 357 171 451 481 13 387 491 489 471 | 439 385 487 177 393 33 71 375 443 129 407 395 127 65 333 309 119 197 472 | 435 497 373 71 379 509 387 159 265 477 463 449 47 353 249 335 505 89 473 | 141 55 235 187 87 363 93 363 101 67 215 321 331 305 261 411 491 479 65 474 | 307 469 415 131 315 487 83 455 19 113 163 503 99 499 251 239 81 167 475 | 391 255 317 363 359 395 419 307 251 267 171 461 183 465 165 163 293 476 | 477 223 403 389 97 335 357 297 19 469 501 249 85 213 311 265 379 297 477 | 283 393 449 463 289 159 289 499 407 129 137 221 43 89 403 271 75 83 478 | 445 453 389 149 143 423 499 317 445 157 137 453 163 87 23 391 119 427 479 | 323 173 89 259 377 511 249 31 363 229 353 329 493 427 57 205 389 91 83 480 | 13 219 439 45 35 371 441 17 267 501 53 25 333 17 201 475 257 417 345 481 | 381 377 55 403 77 389 347 363 211 413 419 5 167 219 201 285 425 11 77 482 | 269 489 281 403 79 425 125 81 331 437 271 397 299 475 271 249 413 233 483 | 261 495 171 69 27 409 21 421 367 81 483 255 15 219 365 497 181 75 431 484 | 99 325 407 229 281 63 83 493 5 113 15 271 37 87 451 299 83 451 311 441 485 | 47 455 47 253 13 109 369 347 11 409 275 63 441 15) 486 | (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 487 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 488 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 519 307 489 | 931 1023 517 771 151 1023 539 725 45 927 707 29 125 371 275 279 817 490 | 389 453 989 1015 29 169 743 99 923 981 181 693 309 227 111 219 897 377 491 | 425 609 227 19 221 143 581 147 919 127 725 793 289 411 835 921 957 443 492 | 349 813 5 105 457 393 539 101 197 697 27 343 515 69 485 383 855 693 493 | 133 87 743 747 475 87 469 763 721 345 479 965 527 121 271 353 467 177 494 | 245 627 113 357 7 691 725 355 889 635 737 429 545 925 357 873 187 351 495 | 677 999 921 477 233 765 495 81 953 479 89 173 473 131 961 411 291 967 496 | 65 511 13 805 945 369 827 295 163 835 259 207 331 29 315 999 133 967 497 | 41 117 677 471 717 881 755 351 723 259 879 455 721 289 149 199 805 987 498 | 851 423 597 129 11 733 549 153 285 451 559 377 109 357 143 693 615 677 499 | 701 475 767 85 229 509 547 151 389 711 785 657 319 509 99 1007 775 359 500 | 697 677 85 497 105 615 891 71 449 835 609 377 693 665 627 215 911 503 501 | 729 131 19 895 199 161 239 633 1013 537 255 23 149 679 1021 595 199 502 | 557 659 251 829 727 439 495 647 223 949 625 87 481 85 799 917 769 949 503 | 739 115 499 945 547 225 1015 469 737 495 353 103 17 665 639 525 75 447 504 | 185 43 729 577 863 735 317 99 17 477 893 537 519 1017 375 297 325 999 505 | 353 343 729 135 489 859 267 141 831 141 893 249 807 53 613 131 547 977 506 | 131 999 175 31 341 739 467 675 241 645 247 391 583 183 973 433 367 131 507 | 467 571 309 385 977 111 917 935 473 345 411 313 97 149 959 841 839 669 508 | 431 51 41 301 247 1015 377 329 945 269 67 979 581 643 823 557 91 405 509 | 117 801 509 347 893 303 227 783 555 867 99 703 111 797 873 541 919 513 510 | 343 319 517 135 871 917 285 663 301 15 763 89 323 757 317 807 309 1013 511 | 345 499 279 711 915 411 281 193 739 365 315 375 809 469 487 621 857 512 | 975 537 939 585 129 625 447 129 1017 133 83 3 415 661 53 115 903 49 79 513 | 55 385 261 345 297 199 385 617 25 515 275 849 401 471 377 661 535 505 514 | 939 465 225 929 219 955 659 441 117 527 427 515 287 191 33 389 197 825 515 | 63 417 949 35 571 9 131 609 439 95 19 569 893 451 397 971 801 125 471 516 | 187 257 67 949 621 453 411 621 955 309 783 893 597 377 753 145 637 941 517 | 593 317 555 375 575 175 403 571 555 109 377 931 499 649 653 329 279 518 | 271 647 721 665 429 957 803 767 425 477 995 105 495 575 687 385 227 519 | 923 563 723 481 717 111 633 113 369 955 253 321 409 909 367 33 967 453 520 | 863 449 539 781 911 113 7 219 725 1015 971 1021 525 785 873 191 893 521 | 297 507 215 21 153 645 913 755 371 881 113 903 225 49 587 201 927 429 522 | 599 513 97 319 331 833 325 887 139 927 399 163 307 803 169 1019 869 523 | 537 907 479 335 697 479 353 769 787 1023 855 493 883 521 735 297 1011 524 | 991 879 855 591 415 917 375 453 553 189 841 339 211 601 57 765 745 621 525 | 209 875 639 7 595 971 263 1009 201 23 77 621 33 535 963 661 523 263 526 | 917 103 623 231 47 301 549 337 675 189 357 1005 789 189 319 721 1005 527 | 525 675 539 191 813 917 51 167 415 579 755 605 721 837 529 31 327 799 528 | 961 279 409 847 649 241 285 545 407 161 591 73 313 811 17 663 269 261 529 | 37 783 127 917 231 577 975 793 921 343 751 139 221 79 817 393 545 11 530 | 781 71 1 699 767 917 9 107 341 587 903 965 599 507 843 739 579 397 397 531 | 325 775 565 925 75 55 979 931 93 957 857 753 965 795 67 5 87 909 97 532 | 995 271 875 671 613 33 351 69 811 669 729 401 647 241 435 447 721 271 533 | 745 53 775 99 343 451 427 593 339 845 243 345 17 573 421 517 971 499 534 | 435 769 75 203 793 985 343 955 735 523 659 703 303 421 951 405 631 825 535 | 735 433 841 485 49 749 107 669 211 497 143 99 57 277 969 107 397 563 536 | 551 447 381 187 57 405 731 769 923 955 915 737 595 341 253 823 197 321 537 | 315 181 885 497 159 571 981 899 785 947 217 217 135 753 623 565 717 538 | 903 581 955 621 361 869 87 943 907 853 353 335 197 771 433 743 195 91 539 | 1023 63 301 647 205 485 927 1003 987 359 577 147 141 1017 701 273 89 540 | 589 487 859 343 91 847 341 173 287 1003 289 639 983 685 697 35 701 645 541 | 911 501 705 873 763 745 657 559 699 315 347 429 197 165 955 859 167 542 | 303 833 531 473 635 641 195 589 821 205 3 635 371 891 249 123 77 623 543 | 993 401 525 427 71 655 951 357 851 899 535 493 323 1003 343 515 859 544 | 1017 5 423 315 1011 703 41 777 163 95 831 79 975 235 633 723 297 589 545 | 317 679 981 195 399 1003 121 501 155) 546 | (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 547 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 548 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 549 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 550 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 2011 1001 49 825 415 1441 551 | 383 1581 623 1621 1319 1387 619 839 217 75 1955 505 281 1629 1379 53 552 | 1111 1399 301 209 49 155 1647 631 129 1569 335 67 1955 1611 2021 1305 553 | 121 37 877 835 1457 669 1405 935 1735 665 551 789 1543 1267 1027 1 554 | 1911 163 1929 67 1975 1681 1413 191 1711 1307 401 725 1229 1403 1609 555 | 2035 917 921 1789 41 2003 187 67 1635 717 1449 277 1903 1179 363 1211 556 | 1231 647 1261 1029 1485 1309 1149 317 1335 171 243 271 1055 1601 1129 557 | 1653 205 1463 1681 1621 197 951 573 1697 1265 1321 1805 1235 1853 1307 558 | 945 1197 1411 833 273 1517 1747 1095 1345 869 57 1383 221 1713 335 559 | 1751 1141 839 523 1861 1105 389 1177 1877 805 93 1591 423 1835 99 1781 560 | 1515 1909 1011 303 385 1635 357 973 1781 1707 1363 1053 649 1469 623 561 | 1429 1241 1151 1055 503 921 3 349 1149 293 45 303 877 1565 1583 1001 562 | 663 1535 395 1141 1481 1797 643 1507 465 2027 1695 367 937 719 545 563 | 1991 83 819 239 1791 1461 1647 1501 1161 1629 139 1595 1921 1267 1415 564 | 509 347 777 1083 363 269 1015 1809 1105 1429 1471 2019 381 2025 1223 565 | 827 1733 887 1321 803 1951 1297 1995 833 1107 1135 1181 1251 983 1389 566 | 1565 273 137 71 735 1005 933 67 1471 551 457 1667 1729 919 285 1629 567 | 1815 653 1919 1039 531 393 1411 359 221 699 1485 471 1357 1715 595 568 | 1677 153 1903 1281 215 781 543 293 1807 965 1695 443 1985 321 879 1227 569 | 1915 839 1945 1993 1165 51 557 723 1491 817 1237 947 1215 1911 1225 570 | 1965 1889 1503 1177 73 1767 303 177 1897 1401 321 921 217 1779 327 571 | 1889 333 615 1665 1825 1639 237 1205 361 129 1655 983 1089 1171 401 572 | 677 643 749 303 1407 1873 1579 1491 1393 1247 789 763 49 5 1607 1891 573 | 735 1557 1909 1765 1777 1127 813 695 97 731 1503 1751 333 769 865 693 574 | 377 1919 957 1359 1627 1039 1783 1065 1665 1917 1947 991 1997 841 459 575 | 221 327 1595 1881 1269 1007 129 1413 475 1105 791 1983 1359 503 691 576 | 659 691 343 1375 1919 263 1373 603 1383 297 781 145 285 767 1739 1715 577 | 715 317 1333 85 831 1615 81 1667 1467 1457 1453 1825 109 387 1207 2039 578 | 213 1351 1329 1173 57 1769 951 183 23 451 1155 1551 2037 811 635 1671 579 | 1451 863 1499 1673 363 1029 1077 1525 277 1023 655 665 1869 1255 965 580 | 277 1601 329 1603 1901 395 65 1307 2029 21 1321 543 1569 1185 1905 581 | 1701 413 2041 1697 725 1417 1847 411 211 915 1891 17 1877 1699 687 582 | 1089 1973 1809 851 1495 1257 63 1323 1307 609 881 1543 177 617 1505 583 | 1747 1537 925 183 77 1723 1877 1703 397 459 521 257 1177 389 1947 1553 584 | 1583 1831 261 485 289 1281 1543 1591 1123 573 821 1065 1933 1373 2005 585 | 905 207 173 1573 1597 573 1883 1795 1499 1743 553 335 333 1645 791 871 586 | 1157 969 557 141 223 1129 1685 423 1069 391 99 95 1847 531 1859 1833 587 | 1833 341 237 1997 1799 409 431 1917 363 335 1039 1085 1657 1975 1527 588 | 1111 659 389 899 595 1439 1861 1979 1569 1087 1009 165 1895 1481 1583 589 | 29 1193 1673 1075 301 1081 1377 1747 1497 1103 1789 887 739 1577 313 590 | 1367 1299 1801 1131 1837 73 1865 1065 843 635 55 1655 913 1037 223 591 | 1871 1161 461 479 511 1721 1107 389 151 35 375 1099 937 1185 1701 769 592 | 639 1633 1609 379 1613 2031 685 289 975 671 1599 1447 871 647 99 139 593 | 1427 959 89 117 841 891 1959 223 1697 1145 499 1435 1809 1413 1445 594 | 1675 171 1073 1349 1545 2039 1027 1563 859 215 1673 1919 1633 779 411 595 | 1845 1477 1489 447 1545 351 1989 495 183 1639 1385 1805 1097 1249 1431 596 | 1571 591 697 1509 709 31 1563 165 513 1425 1299 1081 145 1841 1211 941 597 | 609 845 1169 1865 1593 347 293 1277 157 211 93 1679 1799 527 41 473 598 | 563 187 1525 575 1579 857 703 1211 647 709 981 285 697 163 981 153 599 | 1515 47 1553 599 225 1147 381 135 821 1965 609 1033 983 503 1117 327 600 | 453 2005 1257 343 1649 1199 599 1877 569 695 1587 1475 187 973 233 511 601 | 51 1083 665 1321 531 1875 1939 859 1507 1979 1203 1965 737 921 1565 602 | 1943 819 223 365 167 1705 413 1577 745 1573 655 1633 1003 91 1123 477 603 | 1741 1663 35 715 37 1513 815 941 1379 263 1831 1735 1111 1449 353 1941 604 | 1655 1349 877 285 1723 125 1753 985 723 175 439 791 1051 1261 717 1555 605 | 1757 1777 577 1583 1957 873 331 1163 313 1 1963 963 1905 821 1677 185 606 | 709 545 1723 215 1885 1249 583 1803 839 885 485 413 1767 425 129 1035 607 | 329 1263 1881 1779 1565 359 367 453 707 1419 831 1889 887 1871 1869 608 | 747 223 1547 1799 433 1441 553 2021 1303 1505 1735 1619 1065 1161 2047 609 | 347 867 881 1447 329 781 1065 219 589 645 1257 1833 749 1841 1733 1179 610 | 1191 1025 1639 1955 1423 1685 1711 493 549 783 1653 397 895 233 759 611 | 1505 677 1449 1573 1297 1821 1691 791 289 1187 867 1535 575 183) 612 | (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 613 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 614 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 615 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 616 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 617 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 618 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 619 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 620 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 621 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3915 97 3047 937 2897 953 622 | 127 1201 3819 193 2053 3061 3759 1553 2007 2493 603 3343 3751 1059 783 623 | 1789 1589 283 1093 3919 2747 277 2605 2169 2905 721 4069 233 261 1137 624 | 3993 3619 2881 1275 3865 1299 3757 1193 733 993 1153 2945 3163 3179 625 | 437 271 3493 3971 1005 2615 2253 1131 585 2775 2171 2383 2937 2447 626 | 1745 663 1515 3767 2709 1767 3185 3017 2815 1829 87 3341 793 2627 2169 627 | 1875 3745 367 3783 783 827 3253 2639 2955 3539 1579 2109 379 2939 3019 628 | 1999 2253 2911 3733 481 1767 1055 4019 4085 105 1829 2097 2379 1567 629 | 2713 737 3423 3941 2659 3961 1755 3613 1937 1559 2287 2743 67 2859 325 630 | 2601 1149 3259 2403 3947 2011 175 3389 3915 1315 2447 141 359 3609 631 | 3933 729 2051 1755 2149 2107 1741 1051 3681 471 1055 845 257 1559 1061 632 | 2803 2219 1315 1369 3211 4027 105 11 1077 2857 337 3553 3503 3917 2665 633 | 3823 3403 3711 2085 1103 1641 701 4095 2883 1435 653 2363 1597 767 869 634 | 1825 1117 1297 501 505 149 873 2673 551 1499 2793 3277 2143 3663 533 635 | 3991 575 1877 1009 3929 473 3009 2595 3249 675 3593 2453 1567 973 595 636 | 1335 1715 589 85 2265 3069 461 1659 2627 1307 1731 1501 1699 3545 3803 637 | 2157 453 2813 2047 2999 3841 2361 1079 573 69 1363 1597 3427 2899 2771 638 | 1327 1117 1523 3521 2393 2537 1979 3179 683 2453 453 1227 779 671 3483 639 | 2135 3139 3381 3945 57 1541 3405 3381 2371 2879 1985 987 3017 3031 640 | 3839 1401 3749 2977 681 1175 1519 3355 907 117 771 3741 3337 1743 1227 641 | 3335 2755 1909 3603 2397 653 87 2025 2617 3257 287 3051 3809 897 2215 642 | 63 2043 1757 3671 297 3131 1305 293 3865 3173 3397 2269 3673 717 3041 643 | 3341 3595 3819 2871 3973 1129 513 871 1485 3977 2473 1171 1143 3063 644 | 3547 2183 3993 133 2529 2699 233 2355 231 3241 611 1309 3829 1839 1495 645 | 301 1169 1613 2673 243 3601 3669 2813 2671 2679 3463 2477 1795 617 646 | 2317 1855 1057 1703 1761 2515 801 1205 1311 473 3963 697 1221 251 381 647 | 3887 1761 3093 3721 2079 4085 379 3601 3845 433 1781 29 1897 1599 2163 648 | 75 3475 3957 1641 3911 2959 2833 1279 1099 403 799 2183 2699 1711 2037 649 | 727 289 1785 1575 3633 2367 1261 3953 1735 171 1959 2867 859 2951 3211 650 | 15 1279 1323 599 1651 3951 1011 315 3513 3351 1725 3793 2399 287 4017 651 | 3571 1007 541 3115 429 1585 1285 755 1211 3047 915 3611 2697 2129 3669 652 | 81 3939 2437 915 779 3567 3701 2479 3807 1893 3927 2619 2543 3633 2007 653 | 3857 3837 487 1769 3759 3105 2727 3155 2479 1341 1657 2767 2541 577 654 | 2105 799 17 2871 3637 953 65 69 2897 3841 3559 4067 2335 3409 1087 425 655 | 2813 1705 1701 1237 821 1375 3673 2693 3925 1541 1871 2285 847 4035 656 | 1101 2029 855 2733 2503 121 2855 1069 3463 3505 1539 607 1349 575 2301 657 | 2321 1101 333 291 2171 4085 2173 2541 1195 925 4039 1379 699 1979 275 658 | 953 1755 1643 325 101 2263 3329 3673 3413 1977 2727 2313 1419 887 609 659 | 2475 591 2613 2081 3805 3435 2409 111 3557 3607 903 231 3059 473 2959 660 | 2925 3861 2043 3887 351 2865 369 1377 2639 1261 3625 3279 2201 2949 661 | 3049 449 1297 897 1891 411 2773 749 2753 1825 853 2775 3547 3923 3923 662 | 987 3723 2189 3877 3577 297 2763 1845 3083 2951 483 2169 3985 245 3655 663 | 3441 1023 235 835 3693 3585 327 1003 543 3059 2637 2923 87 3617 1031 664 | 1043 903 2913 2177 2641 3279 389 2009 525 4085 3299 987 2409 813 2683 665 | 373 2695 3775 2375 1119 2791 223 325 587 1379 2877 2867 3793 655 831 666 | 3425 1663 1681 2657 1865 3943 2977 1979 2271 3247 1267 1747 811 159 667 | 429 2001 1195 3065 553 1499 3529 1081 2877 3077 845 1793 2409 3995 668 | 2559 4081 1195 2955 1117 1409 785 287 1521 1607 85 3055 3123 2533 2329 669 | 3477 799 3683 3715 337 3139 3311 431 3511 2299 365 2941 3067 1331 1081 670 | 1097 2853 2299 495 1745 749 3819 619 1059 3559 183 3743 723 949 3501 671 | 733 2599 3983 3961 911 1899 985 2493 1795 653 157 433 2361 3093 3119 672 | 3679 2367 1701 1445 1321 2397 1241 3305 3985 2349 4067 3805 3073 2837 673 | 1567 3783 451 2441 1181 487 543 1201 3735 2517 733 1535 2175 3613 674 | 3019) 675 | (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 676 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 677 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 678 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 679 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 680 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 681 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 682 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 683 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 684 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 685 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 686 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 687 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 688 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2319 653 1379 1675 689 | 1951 7075 2087 7147 1427 893 171 2019 7235 5697 3615 1961 7517 6849 690 | 2893 1883 2863 2173 4543 73 381 3893 6045 1643 7669 1027 1549 3983 691 | 1985 6589 7497 2745 2375 7047 1117 1171 1975 5199 3915 3695 8113 4303 692 | 3773 7705 6855 1675 2245 2817 1719 569 1021 2077 5945 1833 2631 4851 693 | 6371 833 7987 331 1899 8093 6719 6903 5903 5657 5007 2689 6637 2675 694 | 1645 1819 689 6709 7717 6295 7013 7695 3705 7069 2621 3631 6571 6259 695 | 7261 3397 7645 1115 4753 2047 7579 2271 5403 4911 7629 4225 1209 6955 696 | 6951 1829 5579 5231 1783 4285 7425 599 5785 3275 5643 2263 657 6769 697 | 6261 1251 3249 4447 4111 3991 1215 131 4397 3487 7585 5565 7199 3573 698 | 7105 7409 1671 949 3889 5971 3333 225 3647 5403 3409 7459 6879 5789 699 | 6567 5581 4919 1927 4407 8085 4691 611 3005 591 753 589 171 5729 5891 700 | 1033 3049 6567 5257 8003 1757 4489 4923 6379 5171 1757 689 3081 1389 701 | 4113 455 2761 847 7575 5829 633 6629 1103 7635 803 6175 6587 2711 3879 702 | 67 1179 4761 7281 1557 3379 2459 4273 4127 7147 35 3549 395 3735 5787 703 | 4179 5889 5057 7473 4713 2133 2897 1841 2125 1029 1695 6523 1143 5105 704 | 7133 3351 2775 3971 4503 7589 5155 4305 1641 4717 2427 5617 1267 399 705 | 5831 4305 4241 3395 3045 4899 1713 171 411 7099 5473 5209 1195 1077 706 | 1309 2953 7343 4887 3229 6759 6721 6775 675 4039 2493 7511 3269 4199 707 | 6625 7943 2013 4145 667 513 2303 4591 7941 2741 987 8061 3161 5951 708 | 1431 831 5559 7405 1357 4319 4235 5421 2559 4415 2439 823 1725 6219 709 | 4903 6699 5451 349 7703 2927 7809 6179 1417 5987 3017 4983 3479 4525 710 | 4643 4911 227 5475 2287 5581 6817 1937 1421 4415 7977 1789 3907 6815 711 | 6789 6003 5609 4507 337 7427 7943 3075 6427 1019 7121 4763 81 3587 712 | 2929 1795 8067 2415 1265 4025 5599 4771 3025 2313 6129 7611 6881 5253 713 | 4413 7869 105 3173 1629 2537 1023 4409 7209 4413 7107 7469 33 1955 714 | 2881 5167 6451 4211 179 5573 7879 3387 7759 5455 7157 1891 5683 5689 715 | 6535 3109 6555 6873 1249 4251 6437 49 2745 1201 7327 4179 6783 623 716 | 2779 5963 2585 6927 5333 4033 285 7467 4443 4917 3 4319 5517 3449 813 717 | 5499 2515 5771 3357 2073 4395 4925 2643 7215 5817 1199 1597 1619 7535 718 | 4833 609 4797 8171 6847 793 6757 8165 3371 2431 5235 4739 7703 7223 719 | 6525 5891 5605 4433 3533 5267 5125 5037 225 6717 1121 5741 2013 4327 720 | 4839 569 5227 7677 4315 2391 5551 859 3627 6377 3903 4311 6527 7573 721 | 4905 7731 1909 1555 3279 1949 1887 6675 5509 2033 5473 3539 5033 5935 722 | 6095 4761 1771 1271 1717 4415 5083 6277 3147 7695 2461 4783 4539 5833 723 | 5583 651 1419 2605 5511 3913 5795 2333 2329 4431 3725 6069 2699 7055 724 | 6879 1017 3121 2547 4603 2385 6915 6103 5669 7833 2001 4287 6619 955 725 | 2761 5711 6291 3415 3909 2841 5627 4939 7671 6059 6275 6517 1931 4583 726 | 7301 1267 7509 1435 2169 6939 3515 2985 2787 2123 1969 3307 353 4359 727 | 7059 5273 5873 6657 6765 6229 3179 1583 6237 2155 371 273 7491 3309 728 | 6805 3015 6831 7819 713 4747 3935 4109 1311 709 3089 7059 4247 2989 729 | 1509 4919 1841 3045 3821 6929 4655 1333 6429 6649 2131 5265 1051 261 730 | 8057 3379 2179 1993 5655 3063 6381 3587 7417 1579 1541 2107 5085 2873 731 | 6141 955 3537 2157 841 1999 1465 5171 5651 1535 7235 4349 1263 1453 732 | 1005 6893 2919 1947 1635 3963 397 969 4569 655 6737 2995 7235 7713 973 733 | 4821 2377 1673 1 6541))) 734 | 735 | (defun rightzero32 (n) 736 | (declare (optimize speed (safety 0))) 737 | (declare (type (unsigned-byte 32) n)) 738 | (loop for i from 0 below 32 739 | do (unless (logbitp i n) 740 | (return i)))) 741 | 742 | (macrolet ((m (j i) 743 | `(aref mdata (+ ,i (aref m ,j))))) 744 | (define-generator sobol (make-list (sobol-dim generator) :initial-element 'single-float) (stateful-generator) 745 | ((dim 3 :type (unsigned-byte 16)) 746 | (mdata (make-array 0 :element-type '(unsigned-byte 32)) :type (simple-array (unsigned-byte 32) (*))) 747 | (m (make-array 32 :element-type '(unsigned-byte 8)) :type (simple-array (unsigned-byte 8) (32))) 748 | (x (make-array 0 :element-type '(unsigned-byte 32)) :type (simple-array (unsigned-byte 32) (*))) 749 | (b (make-array 0 :element-type '(unsigned-byte 32)) :type (simple-array (unsigned-byte 32) (*))) 750 | (n 0 :type (unsigned-byte 32))) 751 | (:reseed 752 | (assert (< 0 dim 1111)) 753 | (setf mdata (make-array (* 32 dim) :element-type '(unsigned-byte 32))) 754 | (setf x (make-array dim :element-type '(unsigned-byte 32))) 755 | (setf b (make-array dim :element-type '(unsigned-byte 32))) 756 | (dotimes (j 32) 757 | (setf (aref m j) (* j 2)) 758 | (setf (m j 0) 1)) 759 | (loop for i from 1 below dim 760 | for a = (aref sobol-a (1- i)) 761 | for d = 0 762 | do (loop while (< 0 a) 763 | do (incf d) 764 | (setf a (ash a -1))) 765 | (decf d) 766 | (dotimes (j d) 767 | (setf (m j i) (aref sobol-init j (1- i)))) 768 | (loop for j from d below 32 769 | for a = (aref sobol-a (1- i)) 770 | do (setf (m j i) (m (- j d) i)) 771 | (dotimes (k d) 772 | (setf (m j i) (logxor (m j i) (ash (* (logand a 1) (m (+ (- j d) k) i)) 773 | (- d k)))) 774 | (setf a (ash a -1))))) 775 | (dotimes (i dim) 776 | (setf (aref x i) 0) 777 | (setf (aref b i) 0))) 778 | (:next 779 | (declare (optimize speed)) 780 | (let* ((n (update 32 n + 1)) 781 | (m m) (mdata mdata) (x x) (b b) 782 | (c (rightzero32 (incf n)))) 783 | (declare (type (unsigned-byte 8) c)) 784 | (flet ((dim (i) 785 | (declare (type (unsigned-byte 32) i)) 786 | (let ((b_ (aref b i))) 787 | (cond ((<= c b_) 788 | (setf (aref x i) (logxor (aref x i) (ash (m c i) (- b_ c)))) 789 | (float (/ (aref x i) (ash 1 (1+ b_))))) 790 | (T 791 | (setf (aref x i) (logxor (ash (aref x i) (- c b_)) (m c i))) 792 | (setf (aref b i) c) 793 | (float (/ (aref x i) (ash 1 (1+ c))))))))) 794 | (let ((result (make-array dim :element-type 'single-float))) 795 | (dotimes (i (length result) result) 796 | (setf (aref result i) (dim i))))))))) 797 | -------------------------------------------------------------------------------- /squirrel.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator squirrel 32 (hash-generator) () 4 | (:copy 5 | (make-squirrel :%seed (squirrel-%seed generator) 6 | :index (squirrel-index generator))) 7 | (:hash 8 | (declare (optimize speed (safety 1))) 9 | (let ((noise1 #x68E31DA4) 10 | (noise2 #xB5297A4D) 11 | (noise3 #x1B56C4E9) 12 | (bits (fit-bits 32 index))) 13 | (update 32 bits * noise1) 14 | (update 32 bits + (fit-bits 32 seed)) 15 | (update 32 bits logxor (ash bits -8)) 16 | (update 32 bits + noise2) 17 | (update 32 bits logxor (ash bits 8)) 18 | (update 32 bits * noise3) 19 | (update 32 bits logxor (ash bits -8))))) 20 | -------------------------------------------------------------------------------- /staple.ext.lisp: -------------------------------------------------------------------------------- 1 | (asdf:load-system :staple-markless) 2 | -------------------------------------------------------------------------------- /test.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:cl-user) 2 | (defpackage #:org.shirakumo.random-state.test 3 | (:use #:cl #:parachute) 4 | (:local-nicknames 5 | (#:random #:org.shirakumo.random-state))) 6 | (in-package #:org.shirakumo.random-state.test) 7 | 8 | (define-test random-state) 9 | 10 | (defun draw (n generator &optional (arg 1.0)) 11 | (let ((array (make-array n))) 12 | (dotimes (i n array) 13 | (setf (svref array i) (random:random arg generator))))) 14 | 15 | (defun test-clean-copying (generator) 16 | (is equalp 17 | (draw 10 (random:copy generator)) 18 | (draw 10 generator) 19 | "Testing ~a" generator)) 20 | 21 | (defmacro define-default-rng-test (type &body body) 22 | `(define-test ,type 23 | :parent random-state 24 | ,@body 25 | (test-clean-copying (random:make-generator ',type)))) 26 | 27 | (define-default-rng-test random:squirrel) 28 | (define-default-rng-test random:kiss11) 29 | (define-default-rng-test random:tt800) 30 | (define-default-rng-test random:rc4) 31 | (define-default-rng-test random:pcg) 32 | (define-default-rng-test random:middle-square) 33 | (define-default-rng-test random:mersenne-twister-64) 34 | (define-default-rng-test random:mersenne-twister-32) 35 | (define-default-rng-test random:linear-congruence) 36 | (define-default-rng-test random:xorshift-32) 37 | (define-default-rng-test random:xorshift-64) 38 | (define-default-rng-test random:xorshift-128) 39 | (define-default-rng-test random:xorwow) 40 | (define-default-rng-test random:xorshift-64*) 41 | (define-default-rng-test random:xorshift-1024*) 42 | (define-default-rng-test random:xorshift-128+) 43 | (define-default-rng-test random:xoshiro-128**) 44 | (define-default-rng-test random:xoshiro-128+) 45 | (define-default-rng-test random:xoshiro-256**) 46 | (define-default-rng-test random:xoshiro-256+) 47 | 48 | (define-test pcg-ref ;; compare to values from reference implementation 49 | (let ((r (random:make-generator 'random:pcg))) 50 | (finish (random::pcg-reseed r 123)) 51 | (is = #xa672e978b011d001 (random::pcg-state r)) 52 | (is = 247 (random::pcg-inc r)) 53 | (is = #x81c81ce5 (random::pcg-next r)) 54 | (is = #x49e9aca3 (random::pcg-next r)) 55 | (dotimes (i 30) (random::pcg-next r)) 56 | (is = #x3c67e995 (random::pcg-next r)) 57 | (is = #xd4b18bfdfb1841c4 (random::pcg-state r)) 58 | (is = 247 (random::pcg-inc r)))) 59 | 60 | -------------------------------------------------------------------------------- /toolkit.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (declaim (special *generator*)) 4 | 5 | (declaim (inline fit-bits)) 6 | (declaim (ftype (function ((unsigned-byte 8) (integer 0)) (integer 0)) fit-bits)) 7 | (defun fit-bits (bits x) 8 | (logand x (1- (ash 1 bits)))) 9 | 10 | (define-compiler-macro fit-bits (&whole whole bits x &environment env) 11 | (if (constantp bits env) 12 | `(logand ,x (load-time-value (1- (ash 1 ,bits)))) 13 | whole)) 14 | 15 | (defun byte-array-to-int (array) 16 | (loop with int = 0 17 | for i from 0 below (length array) 18 | do (setf (ldb (byte 8 (* i 8)) int) (aref array i)) 19 | finally (return int))) 20 | 21 | (defun hopefully-sufficiently-random-seed () 22 | (or 23 | #+unix 24 | (ignore-errors 25 | (let ((seq (make-array 8 :element-type '(unsigned-byte 8)))) 26 | (with-open-file (stream #P"/dev/urandom" :element-type '(unsigned-byte 8)) 27 | (read-sequence seq stream)) 28 | (byte-array-to-int seq))) 29 | #+(and win32 sb-dynamic-core) 30 | (ignore-errors 31 | (byte-array-to-int (sb-win32:crypt-gen-random 8))) 32 | (logxor #+sbcl (sb-ext:get-bytes-consed) 33 | (get-internal-real-time) 34 | (get-universal-time)))) 35 | 36 | (defun 32bit-seed-array (size seed) 37 | (declare (optimize speed)) 38 | (let ((array (make-array size :element-type '(unsigned-byte 32)))) 39 | (setf (aref array 0) (fit-bits 32 seed)) 40 | ;; Using generator from: 41 | ;; Line 25 of Table 1 in "The Art of Computer Programming Vol. 2" (2nd Ed.), pp 102 42 | (loop for i from 1 below size 43 | do (setf (aref array i) 44 | (fit-bits 32 (* 69069 (aref array (1- i)))))) 45 | array)) 46 | 47 | (defun 64bit-seed-array (size seed) 48 | (declare (optimize speed)) 49 | (let ((array (make-array size :element-type '(unsigned-byte 64)))) 50 | (setf (aref array 0) (fit-bits 64 seed)) 51 | (loop for i from 1 below size 52 | do (setf (aref array i) 53 | (fit-bits 64 (+ (* 6364136223846793005 54 | (logxor (aref array (1- i)) 55 | (ash (aref array (1- i)) -62))) 56 | i)))) 57 | array)) 58 | 59 | (defun barr (bytes &rest contents) 60 | (make-array (length contents) :element-type `(unsigned-byte ,bytes) :initial-contents contents)) 61 | 62 | (defmacro incfmod (place mod &optional (delta 1)) 63 | `(setf ,place (mod (+ ,place ,delta) ,mod))) 64 | 65 | (defun intern* (&rest args) 66 | (intern (format NIL "~{~a~^-~}" (mapcar #'string args)))) 67 | 68 | (defmacro update (bits place op &rest args) 69 | `(setf ,place (fit-bits ,bits (,op ,place ,@args)))) 70 | 71 | 72 | (defun list-dim (list) 73 | (list* (length list) 74 | (when (listp (first list)) 75 | (list-dim (first list))))) 76 | 77 | (defmacro %arr (type &rest elements) 78 | `(make-array ',(list-dim elements) 79 | :element-type ',type 80 | :initial-contents ',elements)) 81 | 82 | (defmacro define-pregenerated (name contents) 83 | `(progn 84 | (let ((contents ,contents)) 85 | (defun ,name () contents)) 86 | (define-symbol-macro ,name (,name)))) 87 | -------------------------------------------------------------------------------- /tt800.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | ;; Adapted from 4 | ;; http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/C-LANG/tt800.c 5 | 6 | (define-generator tt800 32 (stateful-generator) 7 | ((magic (barr 32 0 #x8ebfd028) :type (simple-array (unsigned-byte 32) (2))) 8 | (n 25 :type (unsigned-byte 8)) 9 | (m 7 :type (unsigned-byte 8)) 10 | (index 0 :type (unsigned-byte 64)) 11 | (matrix (make-array 25 :element-type `(unsigned-byte 32)) :type (simple-array (unsigned-byte 32) (25)))) 12 | (:reseed 13 | (setf matrix (32bit-seed-array n seed))) 14 | (:next 15 | (let ((i 0)) 16 | (flet ((matrix (n) (aref matrix n)) 17 | (magic (n) (aref magic n))) 18 | (declare (inline matrix magic)) 19 | (when (= (the integer index) n) 20 | (loop while (< i (- n m)) 21 | do (setf (aref matrix i) 22 | (logxor (matrix (+ i m)) 23 | (ash (matrix i) -1) 24 | (magic (mod (matrix i) 2)))) 25 | (incf i)) 26 | (loop while (< i n) 27 | do (setf (aref matrix i) 28 | (logxor (matrix (+ i (- m n))) 29 | (ash (matrix i) -1) 30 | (magic (mod (matrix i) 2)))) 31 | (incf i)) 32 | (setf index 0)) 33 | (let ((result (matrix index))) 34 | (declare (type (unsigned-byte 32) result)) 35 | (incf index) 36 | (update 32 result logxor (logand (ash result 7) #x2b5b2500)) 37 | (update 32 result logxor (logand (ash result 15) #xdb8b0000)) 38 | (update 32 result logxor (logand (ash result -16) #xffffffff)) 39 | result))))) 40 | -------------------------------------------------------------------------------- /viewer.lisp: -------------------------------------------------------------------------------- 1 | (defpackage #:org.shirakumo.random-state.viewer 2 | (:use #:cl) 3 | (:local-nicknames 4 | (#:random #:org.shirakumo.random-state)) 5 | (:export 6 | #:generate 7 | #:generate-all)) 8 | 9 | (in-package #:org.shirakumo.random-state.viewer) 10 | 11 | (defun tempdir () 12 | #+windows (merge-pathnames #p"AppData/Local/Temp/" (user-homedir-pathname)) 13 | #+unix #p"/tmp/" 14 | #-(or windows unix) (user-homedir-pathname)) 15 | 16 | (defun random-file (generator) 17 | (make-pathname :name (format NIL "~a-~a" (type-of generator) (get-universal-time)) 18 | :type "png" 19 | :defaults (tempdir))) 20 | 21 | (defun generic-open (path) 22 | #+windows 23 | (uiop:launch-program (list "explorer.exe" (uiop:native-namestring path))) 24 | #+(and unix (not darwin)) 25 | (uiop:launch-program (list "xdg-open" (uiop:native-namestring path))) 26 | #+darwin 27 | (uiop:launch-program (list "open" (uiop:native-namestring path)))) 28 | 29 | (defun generate (generator &key file (size 512) (color :grayscale) (open T)) 30 | "Generates a PNG of random samples from the generator. 31 | 32 | GENERATOR may be T (for the *GENERATOR*) a RANDOM-STATE, a GENERATOR, 33 | or any other argument permissible for MAKE-GENERATOR. 34 | 35 | FILE may be a path to save the file to, or NIL in which case a 36 | temporary file name is chosen for you. 37 | 38 | SIZE must be a positive integer designating the dimensions of the 39 | produced image. 40 | 41 | COLOR must be either :GRAYSCALE or :TRUECOLOR to decide which type of 42 | color information to produce in the resulting image. 43 | 44 | OPEN must be one of: 45 | 46 | NIL --- Just return the path 47 | T --- Open the file in the standard OS viewer 48 | string --- Pass the path as an argument to the given program 49 | 50 | Always returns the actual path the image was written to." 51 | (let* ((generator (typecase generator 52 | ((eql T) random:*generator*) 53 | (random:generator generator) 54 | (random-state generator) 55 | (T (random:make-generator generator)))) 56 | (path (etypecase file 57 | (null (random-file generator)) 58 | (string (pathname file)) 59 | (pathname file))) 60 | (data (make-array (* size size (ecase color (:grayscale 1) (:truecolor 3))) :element-type '(unsigned-byte 8))) 61 | (png (make-instance 'zpng:png :width size :height size :color-type color :image-data data))) 62 | (dotimes (i (length data)) 63 | (setf (aref data i) (round (* 255 (random:random-unit generator))))) 64 | (zpng:write-png png path) 65 | (etypecase open 66 | (null) 67 | ((eql T) (generic-open path)) 68 | (string (uiop:run-program (list open (uiop:native-namestring path))))) 69 | file)) 70 | 71 | (defun generate-all (&key (directory (tempdir)) (size 512) (color :grayscale)) 72 | "Generates a PNG for all existing generators. 73 | 74 | The files are all named after the generator, and placed within 75 | DIRECTORY. 76 | 77 | Returns the DIRECTORY. 78 | 79 | See GENERATE" 80 | (dolist (generator (random:list-generator-types) directory) 81 | (with-simple-restart (contine "Ignore ~a" generator) 82 | (generate generator :file (make-pathname :name (format NIL "~a" generator) :type "png" :defaults directory) 83 | :size size 84 | :color color 85 | :open NIL)))) 86 | -------------------------------------------------------------------------------- /xkcd.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | (define-generator xkcd 32 (generator) () 4 | (:reseed) 5 | (:next 6 | (declare (optimize speed (safety 0))) 7 | ;; Chosen fairly by dice roll. 8 | ;; Guaranteed to be random. 9 | 4)) 10 | -------------------------------------------------------------------------------- /xorshift.lisp: -------------------------------------------------------------------------------- 1 | (in-package #:org.shirakumo.random-state) 2 | 3 | ;; Adapted from https://en.wikipedia.org/wiki/Xorshift and Marsaglia, "Xorshift RNGs" 4 | ;; https://www.jstatsoft.org/article/view/v008i14 5 | ;; Xoshiro adapted from https://prng.di.unimi.it/ by Sebastiano Vigna 6 | 7 | ;; Linear variations 8 | 9 | (declaim (ftype (function (integer) (unsigned-byte 64)) splitmix64)) 10 | (defun splitmix64 (seed) ;; Ensures there are no zero seed values 11 | (declare (type integer seed)) 12 | (let ((result (fit-bits 64 seed))) 13 | (declare (type (unsigned-byte 64) result)) 14 | (update 64 result + #x9E3779B97f4A7C15) 15 | (update 64 result logxor (ash result -30)) 16 | (update 64 result * #xBF58476D1CE4E5B9) 17 | (update 64 result logxor (ash result -27)) 18 | (update 64 result * #x94D049BB133111EB) 19 | (update 64 result logxor (ash result -31)) 20 | result)) 21 | 22 | (declaim (ftype (function ((unsigned-byte 8) integer) (simple-array (unsigned-byte 64) (*))) 23 | splitmix64-array)) 24 | (defun splitmix64-array (count seed) 25 | (declare (type (unsigned-byte 8) count)) 26 | (declare (type integer seed)) 27 | (let ((seeds (64bit-seed-array count seed))) 28 | (declare (type (simple-array (unsigned-byte 64) (*)) seeds)) 29 | (loop for i from 0 below (array-dimension seeds 0) 30 | do (setf (aref seeds i) (splitmix64 (aref seeds i))) 31 | finally (return seeds)))) 32 | 33 | (declaim (ftype (function ((unsigned-byte 8) integer) 34 | (simple-array (unsigned-byte 32) (*))) 35 | splitmix32-array)) 36 | (defun splitmix32-array (count seed) 37 | (declare (type (unsigned-byte 8) count)) 38 | (declare (type integer seed)) 39 | (let ((seeds (64bit-seed-array (ceiling count 2) seed))) 40 | (declare (type (simple-array (unsigned-byte 64) (*)) seeds)) 41 | (loop with result = (make-array count :element-type '(unsigned-byte 32)) 42 | for i from 0 below (array-dimension seeds 0) 43 | for j from 0 below count by 2 44 | for seed = (splitmix64 (aref seeds i)) 45 | do (setf (aref result j) (fit-bits 32 seed)) 46 | when (< (1+ j) count) 47 | do (setf (aref result (1+ j)) (fit-bits 32 (ash seed -32))) 48 | finally (return result)))) 49 | 50 | (defstruct (xorshift-generator 51 | (:include stateful-generator) 52 | (:constructor NIL) 53 | (:predicate NIL)) 54 | (xorshifts (make-array 3 :element-type '(signed-byte 16)) 55 | :type (simple-array (signed-byte 16) (3)))) 56 | 57 | (declaim (inline xorshifts)) 58 | (declaim (ftype (function (&rest (signed-byte 16)) (simple-array (signed-byte 16) (3))) xorshifts)) 59 | (defun xorshifts (&rest shifts) 60 | (declare (type list shifts)) 61 | (make-array 3 :element-type '(signed-byte 16) :initial-contents shifts)) 62 | 63 | (defmacro %xorshift (bits place &optional value) 64 | (let ((shifts-var (gensym "XORSHIFT-SHIFTS")) 65 | (count-a-var (gensym "XORSHIFT-COUNT-A")) 66 | (count-b-var (gensym "XORSHIFT-COUNT-B")) 67 | (count-c-var (gensym "XORSHIFT-COUNT-C"))) 68 | `(let* ((,shifts-var (xorshift-generator-xorshifts generator)) 69 | (,count-a-var (aref ,shifts-var 0)) 70 | (,count-b-var (aref ,shifts-var 1)) 71 | (,count-c-var (aref ,shifts-var 2))) 72 | (xorshift-generator-xorshifts generator) 73 | (update ,bits ,place logxor (fit-bits ,bits (ash ,place ,count-a-var))) 74 | (update ,bits ,place logxor (fit-bits ,bits (ash ,place ,count-b-var))) 75 | ,(if value 76 | `(update ,bits ,place logxor value (fit-bits ,bits (ash ,value ,count-c-var))) 77 | `(update ,bits ,place logxor (fit-bits ,bits (ash ,place ,count-c-var))))))) 78 | 79 | (define-generator xorshift-32 32 (xorshift-generator (xorshifts (xorshifts 13 -17 5))) 80 | ((value 0 :type (unsigned-byte 32))) 81 | (:reseed (setf value (fit-bits 32 (splitmix64 seed)))) 82 | (:next ;; Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" 83 | (%xorshift 32 value))) 84 | 85 | (define-generator xorshift-64 64 (xorshift-generator (xorshifts (xorshifts 13 -7 17))) 86 | ((value 0 :type (unsigned-byte 64))) 87 | (:reseed (setf value (splitmix64 seed))) 88 | (:next ;; Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" 89 | (%xorshift 64 value))) 90 | 91 | (define-generator xorshift-128 32 (xorshift-generator (xorshifts (xorshifts 11 -8 -19))) 92 | ((values (make-array 4 :element-type '(unsigned-byte 32)) 93 | :type (simple-array (unsigned-byte 32) (4)))) 94 | (:reseed 95 | (setf values (splitmix32-array 4 seed))) 96 | (:next ;; Algorithm "xor128" from p. 5 of Marsaglia, "Xorshift RNGs" 97 | (let ((temp (aref values 3)) 98 | (value (aref values 0))) 99 | (declare (type (unsigned-byte 32) temp value)) 100 | (setf (aref values 3) (aref values 2)) 101 | (setf (aref values 2) (aref values 1)) 102 | (setf (aref values 1) value) 103 | (setf (aref values 0) (%xorshift 32 temp value)) 104 | (aref values 0)))) 105 | 106 | ;; Non-linear variations 107 | 108 | (define-generator xorwow 32 (xorshift-generator (xorshifts (xorshifts -2 1 4))) 109 | ((counter 0 :type (unsigned-byte 32)) 110 | (values (make-array 5 :element-type '(unsigned-byte 32)) 111 | :type (simple-array (unsigned-byte 32) (5)))) 112 | (:reseed 113 | (setf values (splitmix32-array 5 seed)) 114 | (setf (aref values 4) 0)) 115 | (:next ;; Algorithm "xor128" from p. 5 of Marsaglia, "Xorshift RNGs" 116 | (let ((temp (aref values 4)) 117 | (value (aref values 0))) 118 | (declare (type (unsigned-byte 32) temp value)) 119 | (setf (aref values 4) (aref values 3)) ;; 32 bit shift 120 | (setf (aref values 3) (aref values 2)) 121 | (setf (aref values 2) (aref values 1)) 122 | (setf (aref values 1) value) 123 | (setf (aref values 0) (%xorshift 32 temp value)) 124 | (update 32 counter + 362437) 125 | (fit-bits 32 (+ temp counter))))) 126 | 127 | (define-generator xorshift-64* 64 (xorshift-generator (xorshifts (xorshifts -12 25 -27))) 128 | ((value 0 :type (unsigned-byte 64)) 129 | (magic #x2545f4914f6cdd1d :type (unsigned-byte 64))) 130 | (:reseed (setf value (splitmix64 seed))) 131 | (:next 132 | ;; variant A_1(12,25,27) with multiplier M_32 from line 3 of table 5 of Marsaglia, "Xorshift RNGs" 133 | (%xorshift 64 value) 134 | (fit-bits 64 (* value magic)))) 135 | 136 | (define-generator xorshift-1024* 64 (xorshift-generator (xorshifts (xorshifts 31 -11 30))) 137 | ((index 0 :type (unsigned-byte 4)) 138 | (magic 1181783497276652981 :type (unsigned-byte 64)) 139 | (values (make-array 16 :element-type '(unsigned-byte 64)) 140 | :type (simple-array (unsigned-byte 64) (16)))) 141 | (:reseed 142 | (setf values (splitmix64-array 16 seed)) 143 | (setf index 0)) 144 | (:next ;; Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" 145 | (let ((value (aref values index)) 146 | (temp (aref values (incfmod index #x10)))) 147 | (declare (type (unsigned-byte 64) temp value)) 148 | (%xorshift 64 temp value) 149 | (setf (aref values index) temp) 150 | (fit-bits 64 (* temp magic))))) 151 | 152 | (define-generator xorshift-128+ 64 (xorshift-generator (xorshifts (xorshifts 23 -18 -5))) 153 | ((values (make-array 2 :element-type '(unsigned-byte 64)) 154 | :type (simple-array (unsigned-byte 64) (2)))) 155 | (:reseed (setf values (splitmix64-array 2 seed))) 156 | (:next ;; Algorithm "xor128" from p. 5 of Marsaglia, "Xorshift RNGs" 157 | (let ((temp (aref values 0)) 158 | (value (aref values 1))) 159 | (declare (type (unsigned-byte 64) temp value)) 160 | (setf (aref values 0) value) 161 | (setf (aref values 1) (%xorshift 64 temp value)) 162 | (fit-bits 64 (+ temp value))))) 163 | 164 | (defstruct (xoshiro-generator 165 | (:include stateful-generator) 166 | (:constructor NIL) 167 | (:predicate NIL))) 168 | 169 | (declaim (inline xoshiro-rol32)) 170 | (declaim (ftype (function ((unsigned-byte 32) (signed-byte 32)) (unsigned-byte 32)) 171 | xoshiro-rol32)) 172 | (defun xoshiro-rol32 (value count) 173 | (declare (type (unsigned-byte 32) value)) 174 | (declare (type (signed-byte 32) count)) 175 | (logior (fit-bits 32 (ash value count)) (ash value (- count 32)))) 176 | 177 | (declaim (inline xoshiro-rol64)) 178 | (declaim (ftype (function ((unsigned-byte 64) (signed-byte 32)) (unsigned-byte 64)) 179 | xoshiro-rol64)) 180 | (defun xoshiro-rol64 (value count) 181 | (declare (type (unsigned-byte 64) value)) 182 | (declare (type (signed-byte 32) count)) 183 | (logior (fit-bits 64 (ash value count)) (ash value (- count 64)))) 184 | 185 | (defmacro %inner-xoshiro (bits value values) 186 | (let ((values-var (gensym "XOSHIRO-VALUES")) 187 | (result-var (gensym "XOSHIRO-RESULT")) 188 | (temp-var (gensym "XOSHIRO-TEMP")) 189 | (rol-fun (ecase bits (32 'xoshiro-rol32) (64 'xoshiro-rol64)))) 190 | `(let* ((,values-var ,values) 191 | (,result-var ,value) 192 | (,temp-var (fit-bits ,bits (ash (aref ,values-var 1) 193 | ,(ecase bits (32 9) (64 17)))))) 194 | (update ,bits (aref ,values-var 2) logxor (aref ,values-var 0)) 195 | (update ,bits (aref ,values-var 3) logxor (aref ,values-var 1)) 196 | (update ,bits (aref ,values-var 1) logxor (aref ,values-var 2)) 197 | (update ,bits (aref ,values-var 0) logxor (aref ,values-var 3)) 198 | (update ,bits (aref ,values-var 2) logxor ,temp-var) 199 | (update ,bits (aref ,values-var 3) ,rol-fun 200 | ,(ecase bits (32 11) (64 45))) 201 | ,result-var))) 202 | 203 | (define-generator xoshiro-128** 32 (stateful-generator) 204 | ((values (make-array 4 :element-type '(unsigned-byte 32)) 205 | :type (simple-array (unsigned-byte 32) (4)))) 206 | (:reseed 207 | (setf values (splitmix32-array 4 seed))) 208 | (:next ;; Adapted from works by Sebastiano Vigna 209 | (%inner-xoshiro 210 | 32 211 | (fit-bits 32 (* (xoshiro-rol32 (fit-bits 32 (* (aref values 1) 5)) 7) 9)) 212 | values))) 213 | 214 | (define-generator xoshiro-128++ 32 (stateful-generator) 215 | ((values (make-array 4 :element-type '(unsigned-byte 32)) 216 | :type (simple-array (unsigned-byte 32) (4)))) 217 | (:reseed 218 | (setf values (splitmix32-array 4 seed))) 219 | (:next ;; Adapted from works by Sebastiano Vigna 220 | (%inner-xoshiro 221 | 32 222 | (fit-bits 32 (+ (xoshiro-rol32 (fit-bits 32 (+ (aref values 0) (aref values 3))) 7) 223 | (aref values 0))) 224 | values))) 225 | 226 | (define-generator xoshiro-128+ 32 (stateful-generator) 227 | ((values (make-array 4 :element-type '(unsigned-byte 32)) 228 | :type (simple-array (unsigned-byte 32) (4)))) 229 | (:reseed 230 | (setf values (splitmix32-array 4 seed))) 231 | (:next ;; Adapted from works by Sebastiano Vigna 232 | (%inner-xoshiro 32 (fit-bits 32 (+ (aref values 0) (aref values 3))) values))) 233 | 234 | (define-generator xoshiro-256** 64 (stateful-generator) 235 | ((values (make-array 4 :element-type '(unsigned-byte 64)) 236 | :type (simple-array (unsigned-byte 64) (4)))) 237 | (:reseed 238 | (setf values (splitmix64-array 4 seed))) 239 | (:next ;; Adapted from works by Sebastiano Vigna 240 | (%inner-xoshiro 241 | 64 242 | (fit-bits 64 (* (xoshiro-rol64 (fit-bits 64 (* (aref values 1) 5)) 7) 9)) 243 | values))) 244 | 245 | (define-generator xoshiro-256++ 64 (stateful-generator) 246 | ((values (make-array 4 :element-type '(unsigned-byte 64)) 247 | :type (simple-array (unsigned-byte 64) (4)))) 248 | (:reseed 249 | (setf values (splitmix64-array 4 seed))) 250 | (:next ;; Adapted from works by Sebastiano Vigna 251 | (%inner-xoshiro 252 | 64 253 | (fit-bits 64 (+ (xoshiro-rol64 (fit-bits 64 (+ (aref values 0) (aref values 3))) 23) 254 | (aref values 0))) 255 | values))) 256 | 257 | (define-generator xoshiro-256+ 64 (stateful-generator) 258 | ((values (make-array 4 :element-type '(unsigned-byte 64)) 259 | :type (simple-array (unsigned-byte 64) (4)))) 260 | (:reseed 261 | (setf values (splitmix64-array 4 seed))) 262 | (:next ;; Adapted from works by Sebastiano Vigna 263 | (%inner-xoshiro 64 (fit-bits 64 (+ (aref values 0) (aref values 3))) values))) 264 | 265 | ;; Xoroshiro variants. 266 | 267 | (define-generator xoroshiro-64* 32 (stateful-generator) 268 | ((magic #x9e3779bb) 269 | (values (make-array 2 :element-type '(unsigned-byte 32)) 270 | :type (simple-array (unsigned-byte 32) (2)))) 271 | (:reseed 272 | (setf values (splitmix32-array 2 seed))) 273 | (:next ;; Adapted from works by Sebastiano Vigna 274 | (let* ((temp (aref values 0)) 275 | (value (aref values 1)) 276 | (result (fit-bits 32 (* temp magic)))) 277 | (update 32 temp logxor value) 278 | (setf (aref values 0) (logxor (xoshiro-rol32 temp 26) value (fit-bits 32 (ash value 9)))) 279 | (setf (aref values 1) (xoshiro-rol32 value 13)) 280 | result))) 281 | 282 | (define-generator xoroshiro-64** 32 (stateful-generator) 283 | ((magic #x9e3779bb) 284 | (values (make-array 2 :element-type '(unsigned-byte 32)) 285 | :type (simple-array (unsigned-byte 32) (2)))) 286 | (:reseed 287 | (setf values (splitmix32-array 2 seed))) 288 | (:next ;; Adapted from works by Sebastiano Vigna 289 | (let* ((temp (aref values 0)) 290 | (value (aref values 1)) 291 | (result (fit-bits 32 (* (xoshiro-rol32 (fit-bits 32 (* temp magic)) 5) 5)))) 292 | (update 32 temp logxor value) 293 | (setf (aref values 0) (logxor (xoshiro-rol32 temp 26) value (fit-bits 32 (ash value 9)))) 294 | (setf (aref values 1) (xoshiro-rol32 value 13)) 295 | result))) 296 | 297 | (define-generator xoroshiro-128+ 64 (stateful-generator) 298 | ((values (make-array 2 :element-type '(unsigned-byte 64)) 299 | :type (simple-array (unsigned-byte 64) (2)))) 300 | ;; TODO: This algorithm allows a quick generation of 2^64 :next-calls. 301 | ;; See jump() and long_jump() functions in https://prng.di.unimi.it/xoroshiro128plus.c 302 | (:reseed 303 | (setf values (splitmix64-array 2 seed))) 304 | (:next ;; Adapted from works by Sebastiano Vigna 305 | (let* ((temp (aref values 0)) 306 | (value (aref values 1)) 307 | (result (fit-bits 64 (+ temp value)))) 308 | (update 64 temp logxor value) 309 | (setf (aref values 0) (logxor (xoshiro-rol64 temp 24) value (fit-bits 64 (ash value 16)))) 310 | (setf (aref values 1) (xoshiro-rol64 value 37)) 311 | result))) 312 | 313 | (define-generator xoroshiro-128++ 64 (stateful-generator) 314 | ((values (make-array 2 :element-type '(unsigned-byte 64)) 315 | :type (simple-array (unsigned-byte 64) (2)))) 316 | ;; TODO: This algorithm allows a quick generation of 2^64 :next-calls. 317 | ;; See jump() and long_jump() functions in https://prng.di.unimi.it/xoroshiro128plusplus.c 318 | (:reseed 319 | (setf values (splitmix64-array 2 seed))) 320 | (:next ;; Adapted from works by Sebastiano Vigna 321 | (let* ((temp (aref values 0)) 322 | (value (aref values 1)) 323 | (result (fit-bits 64 (+ (xoshiro-rol64 (fit-bits 64 (+ temp value)) 17) temp)))) 324 | (update 64 temp logxor value) 325 | (setf (aref values 0) (logxor (xoshiro-rol64 temp 49) value (fit-bits 64 (ash value 21)))) 326 | (setf (aref values 1) (xoshiro-rol64 value 28)) 327 | result))) 328 | 329 | (define-generator xoroshiro-128** 64 (stateful-generator) 330 | ((values (make-array 2 :element-type '(unsigned-byte 64)) 331 | :type (simple-array (unsigned-byte 64) (2)))) 332 | ;; TODO: This algorithm allows a quick generation of 2^64 :next-calls. 333 | ;; See jump() and long_jump() functions in https://prng.di.unimi.it/xoroshiro128starstar.c 334 | (:reseed 335 | (setf values (splitmix64-array 2 seed))) 336 | (:next ;; Adapted from works by Sebastiano Vigna 337 | (let* ((temp (aref values 0)) 338 | (value (aref values 1)) 339 | (result (fit-bits 64 (* (xoshiro-rol64 (fit-bits 64 (* temp 5)) 7) 9)))) 340 | (update 64 temp logxor value) 341 | (setf (aref values 0) (logxor (xoshiro-rol64 temp 24) value (fit-bits 64 (ash value 16)))) 342 | (setf (aref values 1) (xoshiro-rol64 value 37)) 343 | result))) 344 | --------------------------------------------------------------------------------