Skip to main content
GameDev.net gamedev.net
🔒 Locked

Random number generating in PHP & C/C++.

Started by Kanzure Oct 29, 2004 at 9:23 PM 3 replies 1.1k views
Original Post
Kanzure
Kanzure
I want to create a few demos of an idea I have. The general jist of the idea is to generate content based on random numbers, and saving only the seeds (in my case - time()) so I can recreate the generatables at a whim. I don't have time to write the C/C++ version, so I'm wondering.. is there a way in PHP to generate the same number, with the same seed, as in C or C++? (This way, I can use the same code in my project and not have to rewrite half of the thing.) Thanks. :)
Alex Swinney
Alex Swinney
I would say no, but maybe..

If PHP can make the same calls to the same code, then yes. As in, COM..COM is language independent, so if you can get into it you can reproduce the same results from either language. That is, if PHP is able to 'talk' to COM (request an interface).
I WISH SOMEONE WOULD FIX THE DAMNED LOGIN!
choffstein
choffstein
Look up the Linear Congruence Pseudo Random Number Generator (theory). Identical seeds will produce identical random numbers in most PRNGs, but is guarenteed in this method.
MaulingMonkey
MaulingMonkey
Sorry, but this is a case of RTFM (I'm blunt, deal with it). From php.net:

PHP: rand - Manual
PHP: srand - Manual (seeds rand).

It's built into the language, no need to duplicate it. It's been in there since PHP3 so you SHOULD have it. It auto-seeds by default since 4.2.0, reseed manually and you should have no problem.

To find the first link I entered "rand" in the "search for" field, and left the "in the" field at it's default - "function list". srand was found via link on the rand manual page. Apparently these use the libc implementation.

There is also PHP: mt_rand - Manual (and mt_srand - Manual) which use the Mersenne Twister "which will produce random numbers four times faster than what the average libc rand() provides". (php.net , mt_rand documentation)

[Edited by - MaulingMonkey on October 30, 2004 9:27:46 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.