Re: rand(), mt_rand() and limits
| From: | Andrea Faulds | Date: | Mon, 12 Jan 2015 09:31:36 +0000 |
| Subject: | Re: rand(), mt_rand() and limits | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-80413@lists.php.net to get a copy of this message | ||
Hi Rasmus,
> On 12 Jan 2015, at 09:06, Rasmus Lerdorf <rasmus@lerdorf.com> wrote:
>
> Yes, there is plenty of code out there that relies on srand()+rand()
> returning a repeatable sequence of pseudo-random numbers. I have written
> some myself. C devs are very familiar with this behaviour of srand.
C devs are also familiar with the usage of strtok, but it doesn't make an API with global state
any less flawed.
The problem with rand() and srand() is they're used for three different purposes, only one of
which they're terribly fitting for:
- To produce a non-repeatable sequence of random numbers (works OK if srand() isn't used)
- To produce a repeatable sequence of random numbers (works, but only if you and the sole user of
the global random number generator, which is not guaranteed by any means)
> There are tons of uses for this in game-related code. Think of something
> like Minecraft world seeds. I bet Minecraft even uses this, or at least
> the Java version of this, for generating its worlds. The worlds are
> randomly generated, but if you get a particularly cool world you can
> check the seed and give that seed to a friend and your friend can get
> the same world generated. There are entire web sites dedicated to
> sharing cool Minecraft seeds like this.
>
> https://www.google.com/search?q=minecraft+seeds
No, procedural generation (which I've done) is considerably more sophisticated and is done
using a deterministic seeded noise generator. Using random numbers from something like rand() just
gets you garbage.
--
Andrea Faulds
http://ajf.me/