Xorshift
Encyclopedia
Xorshift random number generators form a class of pseudorandom number generator
Pseudorandom number generator
A pseudorandom number generator , also known as a deterministic random bit generator , is an algorithm for generating a sequence of numbers that approximates the properties of random numbers...

s that was discovered by George Marsaglia
George Marsaglia
George Marsaglia was an American mathematician and computer scientist. He established the lattice structure of congruential random number generators in the paper "Random numbers fall mainly in the planes". This phenomenon is sometimes called the Marsaglia effect...

. They generate the next number in their sequence by repeatedly taking the exclusive or of a number with a bit shifted
Logical shift
In computer science, a logical shift is a bitwise operation that shifts all the bits of its operand. Unlike an arithmetic shift, a logical shift does not preserve a number's sign bit or distinguish a number's exponent from its mantissa; every bit in the operand is simply moved a given number of bit...

 version of itself. This makes them extremely fast on modern computer architectures. They are a subclass of Linear feedback shift registers, but their simple implementation typically makes them faster and use less space.

Example Implementation

A C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 version In C, the "^" caret
Caret
Caret usually refers to the spacing symbol ^ in ASCII and other character sets. In Unicode, however, the corresponding character is , whereas the Unicode character named caret is actually a similar but lowered symbol: ....

 represents the Bitwise XOR, and " << " the bit shift
Logical shift
In computer science, a logical shift is a bitwise operation that shifts all the bits of its operand. Unlike an arithmetic shift, a logical shift does not preserve a number's sign bit or distinguish a number's exponent from its mantissa; every bit in the operand is simply moved a given number of bit...

.
of one xorshift algorithm is:


uint32_t xor128(void) {
static uint32_t x = 123456789;
static uint32_t y = 362436069;
static uint32_t z = 521288629;
static uint32_t w = 88675123;
uint32_t t;

t = x ^ (x << 11);
x = y; y = z; z = w;
return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}


This algorithm has a period of and it passes the Diehard tests
Diehard tests
The diehard tests are a battery of statistical tests for measuring the quality of a random number generator. They were developed by George Marsaglia over several years and first published in 1995 on a CD-ROM of random numbers.These are the tests:...

.

The first 50 numbers produced by above algorithm are:

  1. 3 701 687 786
  2. 458 299 110
  3. 2 500 872 618
  4. 3 633 119 408
  5. 516 391 518
  6. 2 377 269 574
  7. 2 599 949 379
  8. 717 229 868
  9. 137 866 584
  10. 395 339 113
  11. 1 301 295 572
  12. 1 728 310 821
  13. 3 538 670 320
  14. 1 187 274 473
  15. 2 316 753 268
  16. 4 061 953 237
  17. 2 129 415 220

  1. 448 488 982
  2. 643 481 932
  3. 934 407 046
  4. 723 553 448
  5. 3 932 869 644
  6. 449 460 396
  7. 2 728 332 712
  8. 2 381 680 799
  9. 830 734 233
  10. 2 059 906 653
  11. 544 153 312
  12. 20 906 778
  13. 795 757 459
  14. 1 755 102 565
  15. 811 349 640
  16. 338 079 0346
  17. 2 498 575 418

  1. 420 990 039
  2. 3 358 478 731
  3. 391 216 208
  4. 3 936 394 860
  5. 1 299 350 043
  6. 4 150 927 415
  7. 1 799 713 142
  8. 2 247 676 300
  9. 1 547 958 642
  10. 4 203 610 453
  11. 3 120 566 707
  12. 4 181 181 390
  13. 3 137 093 107
  14. 821 167 952
  15. 2 328 167 796
  16. 3 450 572 369


External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK