4 ms·
Do be sure to note the mention that the new algorithm is not considered a cryptographically secure PRNG. Also because the specification has very little by way
by dspillett 11y ago
Do be sure to note the mention that the new algorithm is not considered a cryptographically secure PRNG.
Also because the specification has very little by way of requirements in that regard, no matter how good some implementations may be you should always assume you code may end up being used in an environment where Math.random() is no better than the worst generator you can think of.
If you need specific properties in your PRNG then you still need to provide something in place of Math.random().
- danbolt 11y agoThis is good to think about! Often when programming games that need certain properties, I'll write my own toy PRNG, but I'll admit I'd love to learn more about the craft. Would you have any suggested reading material on the topic?
- resc1440 11y agoAs an outsider to the field, I really enjoyed reading this paper: http://www.pcg-random.org/paper.html http://www.pcg-random.org/paper.html It's advocating for a particular new kind of PRNG, but it also includes a lot of great explanations and citations for more reading.
- yoklov 11y agoAs a shameless plug, I wrote a JS port of this RNG algorithm a while back: https://www.npmjs.com/package/pcg-random https://www.npmjs.com/package/pcg-random .
- PhantomGremlin 11y agoany suggested reading material on the topic? The Art of Computer Programming, Vol. 2, (c) 1969, by Donald Knuth, has quite an extensive discussion of random numbers, their properties, algorithms, and tests. It's about 160 pages in my edition. It's very readable, and even if you skip all the exercises you will still get a very good grounding. I don't know how much has changed in his more recent editions. And I don't know how if there are better books. But, in general, you can't go wrong by starting with Knuth. He's one of the all time greats!
- yoklov 11y agoFor games, the most important features (other than the features of an RNG that all applications desire, such as good performance, high period, etc.) are being able to seed the state, and save/restore the state (required for save game support if you need seeding and want that to work after they save and restore the game).
- zeveb 11y agoFor a modern game, is there any particular reason why 'seed = SHA256(seed + "reseed"); return SHA256(seed + "value")' isn't a good enough, performant enough RNG?
- vonmoltke 11y agoFor starters, there is nothing random about it.
- zeveb 11y ago> For starters, there is nothing random about it. Yes, since the original post wanted seedability: > > For games, the most important features (other than the features of an RNG that all applications desire, such as good performance, high period, etc.) are being able to seed the state, and save/restore the state (required for save game support if you need seeding and want that to work after they save and restore the game). Given that restriction, what's wrong with the generator I suggest?
- vonmoltke 11y agoIt's not random. It's not a generator. You are just returning a different representation of the seed you already have.
- zeveb 11y ago> It's not random. It's not a generator. No, it's a pseudorandom generator. It's a key attribute of a cryptographically secure function like SHA256 that its output is indistinguishable from random bits. > You are just returning a different representation of the seed you already have. I'm returning a function of the seed, yes. That's the whole point of a seeded PRNG: that seed(x); rng(); rng(); rng() will always return the same sequence of results. This is used for example in procedural generation, to ensure that a dungeon level or map chunk is always generated the same way (a cool hack, then, is to just store the seed instead of the level itself, and rerun the level-generation algorithm with the seed when one wishes to regenerate the level). In such an application one wants the appearance of randomness, but with repeatability. Which was what the original post asked for. What I suggested is not secure for generating keys or for other cryptographic purposes.
- malingo 11y agoThis is a great 2-part article: http://www.drdobbs.com/tools/fast-high-quality-parallel-random-number/231000484 http://www.drdobbs.com/tools/fast-high-quality-parallel-rand...
- neerdowell 11y ago> the worst generator you can think of Decrementing the seed by 1 with each call[0], which is a standards-compliant rand implementation. Although it looks like a standards-compliant Math.random() implementation isn't allowed to be quite that bad[1]. [0] https://marc.info/?l=openbsd-tech&m=141773078029373&w=2 https://marc.info/?l=openbsd-tech&m=141773078029373&w=2 [1] http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.14 http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.1...
- tlrobinson 11y agoObligatory XKCD: https://xkcd.com/221/ https://xkcd.com/221/
- tlrobinson 11y agoThis is the most important point. I didn't like that "TIFU by using Math.random()" article because it felt like it was deflecting some of the blame by focusing so much on the details of V8's PRNG, when it reality it's 100% his fault for not using a CSPRNG (in a gambling application, no less).
- WatchDog 11y agoHis application was using the RNG to avoid collisions in high performance random ID generation. Without knowing more about the system, it seems like a good case for a good non-CSPRNG.
- jameshart 11y agoAll true. And the 'specific properties' you might need might not be cryptographic security. For example, I did some stuff recently in Javascript with procedural generation that made heavy use of random numbers. I used my own implementation of a mersenne twister, rather than Math.random, precisely so I could have predictable long term and cross-engine repeatability of the random number sequences generated based on the same seed. So how good does the default random number routine in JavaScript really need to be?