DanC Posted September 28, 2009 Share Posted September 28, 2009 Hi, I have this code in my script: $genno=rand(1,50000000000000); // Run some security checks for duplicate ids $genids=mysql_query("SELECT gen FROM info WHERE gen='$genno' "); $checkgenids=mysql_num_rows($genids); while ($checkgenids > 0) { $genno=rand(1,50000000000000); } // End the check $nameids=mysql_query("SELECT name FROM info WHERE name='$name' "); $checknameids=mysql_num_rows($nameids); if ($checknameids > 0) { die('<p><b><center><font color=red>Product name "'.$name.'" already exists!<hr></font></center></b></p>'); } mysql_query("INSERT INTO $maintab ($nametab, $linktab, $comptab) VALUES('$name', '$link', '$comp')") or die(mysql_error()); mysql_query("INSERT into $gcodetab ($codetab) VALUES ('$finalshuffle')") or die(mysql_error()); mysql_query("UPDATE $gcodetab SET gen='$genno' WHERE gen='0'") or die(mysql_error()); mysql_query("INSERT into download (id, gen) VALUES ('$genno', '$genno')") or die(mysql_error()); mysql_query("UPDATE $maintab SET gen='$genno' WHERE gen='fill'") or die(mysql_error()); mysql_free_result(); mysql_close; However, no matter how many times I run this code, it ALWAYS inserts the SAME number into the database. It works perfectly on localhost, but not when I upload it to my online host. I've asked them to check for any restrictions, but they showed me a test with rand function and it was working fine. Is it something in my code that is wrong? It inserts the random code into two tables in a database so I can match them up later, but one table always has the same code and the other table has a different code. Help! Thanks, Dan. Link to comment https://forums.phpfreaks.com/topic/175780-rand-always-generating-same-number/ Share on other sites More sharing options...
ILMV Posted September 28, 2009 Share Posted September 28, 2009 Try using mt_rand() instead... Link to comment https://forums.phpfreaks.com/topic/175780-rand-always-generating-same-number/#findComment-926278 Share on other sites More sharing options...
Mark Baker Posted September 28, 2009 Share Posted September 28, 2009 Whether using rand or mt_rand, the result is an integer value so it is limited by the maximum integer value. 50000000000000 is way, way above this value. Use getrandmax() or mt_getrandmax() to identify the upper limit of random numbers on your platform Link to comment https://forums.phpfreaks.com/topic/175780-rand-always-generating-same-number/#findComment-926320 Share on other sites More sharing options...
Dtonlinegames Posted September 28, 2009 Share Posted September 28, 2009 Looks to me like $genno is set outside of a loop, so its set once and the number will always be more than 0 as you check here and therefore never actually changes while ($checkgenids > 0) { $genno=rand(1,50000000000000); } So you might want to put something at the end of you're code either $genno=''; Or doing a do{}while() like do{ $genno=rand(1,50000000000000); // Run some security checks for duplicate ids $genids=mysql_query("SELECT gen FROM info WHERE gen='$genno' "); $checkgenids=mysql_num_rows($genids); while ($checkgenids > 0) { $genno=rand(1,50000000000000); } // End the check $nameids=mysql_query("SELECT name FROM info WHERE name='$name' "); $checknameids=mysql_num_rows($nameids); if ($checknameids > 0) { die('<p><b><center><font color=red>Product name "'.$name.'" already exists!<hr></font></center></b></p>'); } mysql_query("INSERT INTO $maintab ($nametab, $linktab, $comptab) VALUES('$name', '$link', '$comp')") or die(mysql_error()); mysql_query("INSERT into $gcodetab ($codetab) VALUES ('$finalshuffle')") or die(mysql_error()); mysql_query("UPDATE $gcodetab SET gen='$genno' WHERE gen='0'") or die(mysql_error()); mysql_query("INSERT into download (id, gen) VALUES ('$genno', '$genno')") or die(mysql_error()); mysql_query("UPDATE $maintab SET gen='$genno' WHERE gen='fill'") or die(mysql_error()); mysql_free_result(); } while($genno!==$genno); mysql_close; That will loop that entire function as long as Genno DOESNT equal its last value I havent tested this but it seemed the only option, I had a similar problem once. Many moons ago Link to comment https://forums.phpfreaks.com/topic/175780-rand-always-generating-same-number/#findComment-926341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.