35

If you search for examples of creating a seeded (pseudo)Random number generator, you will run into stuff like this (specific example http://indiegamr.com/generate-repeatable-random-numbers-in-js/):

// the initial seed
Math.seed = 6;

// in order to work 'Math.seed' must NOT be undefined,
// so in any case, you HAVE to provide a Math.seed
Math.seededRandom = function(max, min) {
    max = max || 1;
    min = min || 0;

    Math.seed = (Math.seed * 9301 + 49297) % 233280;
    var rnd = Math.seed / 233280;

    return min + rnd * (max - min);
}

Those specific numbers (9301, 49297, 233280) and algorithm are used over and over, but nobody seems to have a definitive reference for this. Who invented this algorithm and tested the distribution? Is there a paper or something to cite?

7
  • 13
    it's a linear congruent generator but with a fairly small period (only 233k while a 32 bit int allows have a 4 billion period Commented Oct 26, 2014 at 19:22
  • 2
    People often copy code directly from books, so it's probably from an old book somewhere and has been copied several times. It also appears to be a limiting case. Possibly helpful: heydari.persiangig.com/Ebooks/Applied_Crypto-Ch11-ch20.pdf/… ict.griffith.edu.au/anthony/info/C/RandomNumbers
    – user11586
    Commented Oct 27, 2014 at 20:29
  • 2
    Whatever the origin, those are terrible values to use for calculating a seed.
    – user22815
    Commented Oct 27, 2014 at 21:22
  • 6
    @jlarson a comment is not nearly long enough, but there are two issues at hand. First, as ratchet freak alluded to, the modulo is the maximum period: number of unique numbers before the generator repeats itself. The actual period may be smaller. Second, the other two numbers (mostly the multiplicand) should be relatively prime to the modulo number to ensure a longer period. Ideally the modulo number is the largest prime less than the maximum positive integer that fits in the data type, and the other two numbers are also large primes.
    – user22815
    Commented Nov 4, 2014 at 3:15
  • 1
    That is the short, short version of why those numbers are terrible, given this is a side discussion and adding an actual answer is not appropriate for this question. I recommend bouncing around Wikipedia and maybe Mathematics or Computer Science for more info, although technically pseudorandom number algorithms are also on-topic at Programmers.
    – user22815
    Commented Nov 4, 2014 at 3:21

1 Answer 1

26

A quick search of Google Books shows these numbers (9301, 49297, 233280) have been used in a number of references:

  • Numerical Recipes in FORTRAN 77
  • An Introduction to Numerical Methods in C++
  • CGI Developer's Resource: Web Programming in TCL and PERL
  • Effective Fortran 77 for Engineers & Scientists
  • JavaScript development
  • All on C
  • Java Examples in a Nutshell
  • Seminumerical algorithms
  • An Introduction To Mechanics

The oldest is 1977's Computer methods for mathematical computations by George Elmer Forsythe, Michael A. Malcolm, Cleve B. Moler (Prentice-Hall), although Google doesn't show where the text was used in the book so it cannot be verified.

The earliest showing the text is Numerical Recipes in Pascal (First Edition): The Art of Scientific Computing, Volume 1 by Press, Teukolsky, Vetterling and Flannery in a full-page table of "Constants for Portable Random Number Generators". These particular numbers are given with an overflow at 2^31.

The Numerical Recipes series of books are hugely popular, and have been in print since 1986.

11
  • 6
    Wow, if the answer isn't in here I don't know where it would be. Thanks.. // I was kind of hoping to be able to point to some specific research as to why these numbers are special, but this suffices. 9301 is a product of two primes (71x131), 49297 is a prime -- instinctually I feel like that must be relevant. 233280 is not prime -- it equals 2x2x2x2x2x2x3x3x3x3x3x3x5 (or 2^6 * 3^5 * 5)
    – jlarson
    Commented Oct 28, 2014 at 18:47
  • Sometimes these types of numbers, along with seeds, have a personal significance to the author. A birthday, a favorite number, a random but meaningful statistic, or some combination along the same lines. For example, I use 42 as a seed as an homage to Hitchhikers Guide to the Galaxy. Commented Aug 6, 2022 at 15:43
  • Didn't Knuth's Seminumerical Algorithms 1st edition show up in 1969? That seems to predate everything else. I don't have 1st edition to check, but I also cannot find any evidence that this LCG was referenced in at least the 3rd edition I have. I think TAOCP (Seminumerical Algorithms) may be a false positive.
    – Anders
    Commented Aug 6, 2022 at 16:50
  • Yeah the off-beat title "Seminumerical Algorithms" is solely Knuth's, but these numbers don't appear in it: not the current (1997) 3rd edition, not the 1981 2nd edition, and while I don't have access to it right now, I doubt it appeared in the 1969 1st edition either. I've found Google Books often just shows "related" books in which the searched terms don't appear at all. Commented Aug 6, 2022 at 17:08
  • "computer methods for mathematical computations" can be 'borrowed' for free at archive.org/details/computermethodsf00fors/page/240/mode/2up but I did not find the values in that book Commented Aug 6, 2022 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.