Jump to content

Rand() always generating same number


DanC

Recommended Posts

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
Share on other sites

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 :P

 

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 :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.