When running this code on both MAMP and XAMPP on a MacBook Pro (i7) I generate a consistent hash on my mac of: d0182754cd1fc4456679945f92a9d7b9
However when running this on PC this hash is apparently incorrect. Both systems are running the same versions of XAMPP however the windows machines are outputting a hash beginning cc...
What could be causing this problem and how can I rectify this?
[CODE]<?php
// All you have to do is find the code by setting the seed number to 1234
// set the seed
$a = 1234;
srand($a);
//convert the random number into a string then convert into an md5 hash
$b = rand();
$c = strval($b); // Note the md5 function only workd on strings not numbers.
$d = md5($c);
if ($d == "3de36aabe0a47900e74f4a55a03db1fc"){
$d= "You need to change the seed number!";
Echo " ", $d;
}
else {
Echo " ", "SUCCESS!!. This is the code that you enter into Moodle ONCE you have altered the seed number::"," ", $d;
echo "<br>";
}
?>[/CODE]
Windows and Mac systems Are both programmatically and structurally different to each other, so you cannot expect them to generate the same number, even with the same seed. At least that's probably the reason.
Anyway, from now on, please refrain from asking these questions as a thread but instead please post in the "Web Development Questions That Don't Need Their Own Thread" thread.
Seeing the random number generator doesn't mean you'll get the same number. It's random, it's always random.
And mt_rand is better & faster than rand.
I'm still having issues with the code when it is run from different machines on different servers, all of the servers are producing a hash of d0182754cd1fc4456679945f92a9d7b9, the thing I'm finding odd is how different servers on different hardware are producing the same incorrect result.
Would it be possible to see what results are getting on other servers?
I apologise for making a new thread, as it's now started I though it may as well be used.
I'm not quite getting your problem. Like TerabyteS_ said, different platforms will obviously yield different results, you can't expect any manner of consistency dealing with random values, particularly across various systems.
The problem seems to be with the seeded random number generator, for some reason it produces a 9 digit random number whereas the seeded version in Windows produced a 5 digit I think this is something to do with the seeding of a random number.
[QUOTE=Jacko2007;33054619]The problem seems to be with the seeded random number generator, for some reason it produces a 9 digit random number whereas the seeded version in Windows produced a 5 digit I think this is something to do with the seeding of a random number.[/QUOTE]
Exactly:
[QUOTE=TerabyteS_;33053644]Windows and Mac systems Are both programmatically and structurally different to each other, so you cannot expect them to generate the same number, even with the same seed. At least that's probably the reason.[/QUOTE]
[QUOTE=StinkyJoe;33054041]I'm not quite getting your problem. Like TerabyteS_ said, different platforms will obviously yield different results, you can't expect any manner of consistency dealing with random values, particularly across various systems.[/QUOTE]
The problem seems to still be arising when the code is running on different servers including windows servers.
Also should the random function from within PHP be platform independent and also be regarding the seed to generate the number?
Err, no?
[editline]31st October 2011[/editline]
Why do you need it to be the same, anyway?
[QUOTE=TerabyteS_;33054857]Err, no?
[editline]31st October 2011[/editline]
Why do you need it to be the same, anyway?[/QUOTE]
The code is used as part of an assignment whereby a hash is generated based on a random number being created from a seed.
I got a consistent "wrong" answer, but this was due to the random number being generated differently.
Well, you are using a random pseudo-random-function, just saying... Depending on how the pseudo-random generator is implemented on the system you might not get the same random number from the same random seed in the same code usind rand().
You could try using mt_rand as its the implementation of the Mersenne Twister,
using mt will definitely provide equal results cross platform
c:\> php -r "mt_srand(1234); echo mt_rand();"
1741177057
root@srv [~]# php -r 'mt_srand(1234); echo mt_rand(),"\n";'
1741177057
It is stated right here in the PHP manual pages
[url]http://php.net/manual/en/function.mt-rand.php[/url]
"Many random number generators of older libcs have dubious or unknown characteristics and are slow. By default, PHP uses the libc random number generator with the rand() function."
I've got an mt_rand() example working now, however the course tutor is insisting that my hashes are still incorrect and they should start 'cc...'
I don't understand how I am supposed to get my random number generator to act in a way so that
$a = 1234;
srand($a);
$b = rand();
At this point they're expecting the random number ($b) to be 14755 based on that seed, however mine is consistently producing 479142414.
I'm sorry, but if you're doing a course shouldn't you be doing it yourself?
[QUOTE=Jacko2007;33061646]I've got an mt_rand() example working now, however the course tutor is insisting that my hashes are still incorrect and they should start 'cc...'
I don't understand how I am supposed to get my random number generator to act in a way so that
$a = 1234;
srand($a);
$b = rand();
At this point they're expecting the random number ($b) to be 14755 based on that seed, however mine is consistently producing 479142414.[/QUOTE]Tell your tutor he's a dummy.
This a question regarding the different in values not asking you to solve to problem, the code is correct its just the values it's producing are not.
I agree my tutor is being a dummy.
Do this:
[img]http://imgs.xkcd.com/comics/random_number.png[/img]
[editline]1st November 2011[/editline]
Seriously though, I still don't understand what you're expecting us to do.
Sorry, you need to Log In to post a reply to this thread.