Forum Moderators: coopster
Here is an example of what I want the code to do:
$rnum = mysql_query ("SELECT rnum FROM table;");
$random_number = rand(1,10000);
while ($random_number = $rnum) {
$random_number = rand(1,10000); //generate new number if it already exists in the table
}
/* code to add unique number to table */
This doesn't work because rnum = Resource id #3, so $random_number will never equal rnum (because rnum isn't even a number). Is there something I can do to modify this so it works or do I need to do something entirely different?
Thanks.
<added>rnum is a unique field by the way</added>
Like this:
$row['rcnt']=1;
while($row['rcnt']>0)
{
$random_number = rand(1,10000);
$result = mysql_query('SELECT COUNT(rnum) as rcnt FROM table WHERE rnum="'.$random_number.'";');
$row = mysql_fetch_array($result);
} There's probably a more clever way of doing this with just SQL using a unique key.
[edited by: jatar_k at 4:56 pm (utc) on Aug. 25, 2003]
You should generate your random number, and then see if that specific number exists in your table. Use a while loop to keep generating & checking numbers
Take a look at the code in my first post. Is that not what I was doing in the first place? As I said, it was "just an example of what I want the code to do", but isn't it the same method as what you posted?
But thanks for your solution, I'll try it out some time this week.
[edited by: jatar_k at 4:57 pm (utc) on Aug. 25, 2003]
Just as an aside - if you know your random number generator code does xxx numbers without repeating, then why not use that, always seed with the same number, then keep an incriment counter, and get the n'th number, knowing it'll be unique?
depending on how good the random number generator is, and how many numbers you already have
I'm just using rand() to generate the random numbers, nothing fancy. And I don't understand why it matters how many numbers are in the database. As long as it keeps generating numbers until that number isn't in the database, would that be sufficient?
I am randomly generating very large numbers and my database is small.... hmm I think I follow you now. If my database had 10 numbers (1-10) and my script only generated numbers between 1 and 10 I could see why there would be cause for concern. But I can't increment because I'm using these numbers in a way that the n'th number cannot be predetermined. My ID field auto increments but my rnum field needs to be completely random. BTW, what is seeding?
My loop times out after a few seconds so I don't have to worry about it being infinite.
Take a look at the code in my first post. Is that not what I was doing in the first place? As I said, it was "just an example of what I want the code to do", but isn't it the same method as what you posted?
No. It appears you were trying to retrieve all numbers from your database and then comparing those with your random number, except your weren't doing it right. My code compares a pair of numbers at a time.
[edited by: jatar_k at 4:58 pm (utc) on Aug. 25, 2003]
That does give me an idea though. I haven't tried Dolemites code yet but is there a way to AUTO_INCREMENT by a random number? If I could do that to my rnum field as a UNIQUE field, it should keep making random numbers until one is unique.
...is there a way to AUTO_INCREMENT by a random number?
auto-increment + random number = new value
-------------- ------------- ---------
1 + 20 21
2 + 12 14
3 + 76 79
4 + 10 14
5 + 55 60