GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Aug-2004, 10:17
da_bomb50 da_bomb50 is offline
New Member
 
Join Date: Jul 2004
Posts: 21
da_bomb50 is on a distinguished road

Help with random numbers


I 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  
Old 04-Aug-2004, 13:27
da_bomb50 da_bomb50 is offline
New Member
 
Join Date: Jul 2004
Posts: 21
da_bomb50 is on a distinguished road
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  
Old 04-Aug-2004, 16:15
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,370
JdS will become famous soon enough
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

GIDApp | GIDBlog | GIDForums | GIDNetwork | Learning Journal
  #4  
Old 04-Aug-2004, 19:34
da_bomb50 da_bomb50 is offline
New Member
 
Join Date: Jul 2004
Posts: 21
da_bomb50 is on a distinguished road
Thank you very much.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

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 05: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 09:57

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:56.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.