![]() |
|
#1
|
|||
|
|||
Help with random numbersI saw some stuff with C about how to get an unrandom random number but nothing with PHP. What i need it to do is to generate a random number, check a MySQL DB to see if that number is already used, and then if it is, it goes through the loop again and generates another random number. I used a do...while loop for this. Except the loop will never break because the numbers are always identical. How can i generate and unrandom random number with PHP?
|
|
#2
|
|||
|
|||
|
OK, now something really strange is happening. What happens is that it generates the random number then prints it along with other information onto a text file and it also adds the number to the db. The text file number seems to be random but the db number is always the same. How can 1 variable produce 2 different numbers without being changed. I use:
$random = rand(100,999); fwrite($random); mysql_query("INSERT INTO users VALUES('','$random')"); In the db it always produces the number 127 but in the text file it produces a totaly different number. Why? Please help. Last edited by da_bomb50 : 04-Aug-2004 at 13:33.
Reason: some smiley appeared in the middle of my code, lol
|
|
#3
|
||||
|
||||
|
Hello da_bomb50,
I think this has to do with the 2nd column in your `users` table being too small. I will guess that the random number is being inserted into a column that's set up as TINYINT(signed). The max for this column is 127. If you had created the column as TINYINT(unsigned) then the max would still be 255. The solution is to alter the table/column and modify it to be at least a SMALLINT or MEDIUMINT... or even INT, anyone of which can handle the max number in your random limits above in your code example. Also, instead of rand(), I would use mt_rand(). If you want to know why, please refer to the manual, http://www.php.net/manual/en/function.mt-rand.php , where it states that mt_rand() is 4 x faster. __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#4
|
|||
|
|||
|
Thank you very much.
|
Recent GIDBlog
A Week in Kuwait by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Assigning random values | enggwaqas | C++ Forum | 3 | 22-Jun-2004 10:43 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
| Random() : Make each number onlu appear once | NiXeN | C++ Forum | 3 | 13-Jan-2004 04:47 |
| Need a script for selecting random tabels and from those tabels selecting random ques | mlt | MySQL / PHP Forum | 2 | 12-Sep-2003 09:01 |
| Random no longer JUST random... | JdS | GIDTopsites™ | 0 | 12-Jan-2003 08:57 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The