Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Jun 2007
    Location
    UK
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Quickest way to choose a random number NOT already chosen

    I have seen snippets of how to create a random number using php.

    But what would be the most efficient and quick-running method of creating a random number that was not already created (stored in a MYSQL database).

    Just a method please.... I think I can code it

    - Mutasim

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    a random number
    hmm this is a hard one....
    rand();

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    So you're storing the already generated numbers in a database?

    What's the range of numbers you want?

  • #4
    New Coder
    Join Date
    Jun 2007
    Location
    UK
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mr e View Post
    So you're storing the already generated numbers in a database?

    What's the range of numbers you want?
    well 5 digit numbers really so 00001- 99999
    Last edited by mutasim; 06-03-2007 at 09:19 PM.

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    sorry it wasnt sarcasm it was a joke, but that is how you generate a random number that is not stored in a mysql database!

  • #6
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Quote Originally Posted by mutasim View Post
    well 5 digit numbers really so 00001- 99999
    could use
    PHP Code:
    $arr range(00001,99999);
    $i shuffle($arr);
    echo 
    "Random Number is $i"

  • #7
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Do you need to just generate unique random numbers for the duration of the script? Or do you need them to be permanently random?

    If you let us know what you're using them for we might be better able to help you

  • #8
    New Coder
    Join Date
    Sep 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just create a list of numbers in the valid range, then use shuffle, then insert them into file and use fseek to extract the next number in the file, keeping track of the next number to extract in the same file. Random, resource friendly and never will there be a chance that you will return the same number! File list can also auto resets when you get to the end of the file. Great for CAPTCHA security image system.

  • #9
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Unless I'm missing something I'm still not seeing whats wrong with rand().
    PHP Code:
    $randomNumber str_pad(rand(1,99999), 5"0"STR_PAD_LEFT); 
    Use rand to pick a random number between 1 and 99999 and then pad it out with zeros

  • #10
    New Coder
    Join Date
    Jun 2007
    Location
    UK
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It will act as a primary key... so needs to be unique + random

  • #11
    New Coder
    Join Date
    Sep 2006
    Posts
    92
    Thanks
    4
    Thanked 1 Time in 1 Post
    PHP Code:
    $a rand(00001,99999);
    $rows mysql_querySELECT count(*) from table where key $a );
    while ( 
    $rows != ) {
         
    $a rand(00001,99999);
         
    $rows mysql_querySELECT count(*) from table where key $a );

    something like that, just generate a number and see if it exists. If yes, generate a new number. I must say though that sooner or later this will lead to trouble as well I guess.

  • #12
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    I'm just going out on a limb here, but if you're wanting to select random rows from a table, you can do "ORDER BY RAND() LIMIT 5" for example

    Otherwise perhaps having one field with all the numbers you've used separated by commas, then you could query that one field, explode the numbers, and while your randomly generated number is in the exploded array, generate a new random number, when you fail to meet the condition you'll have a unique random number

    Or just query for every id (or whatever your field is called) and loop through and add all those id's to an array, then continue with the above method

  • #13
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by twomt View Post
    PHP Code:
    $a rand(00001,99999);
    $rows mysql_querySELECT count(*) from table where key $a );
    while ( 
    $rows != ) {
         
    $a rand(00001,99999);
         
    $rows mysql_querySELECT count(*) from table where key $a );

    something like that, just generate a number and see if it exists. If yes, generate a new number. I must say though that sooner or later this will lead to trouble as well I guess.
    You realize in a worst case scenario you just made 10,000 queries right

  • #14
    New Coder
    Join Date
    Sep 2006
    Posts
    92
    Thanks
    4
    Thanked 1 Time in 1 Post
    As I said, sooner or later this will lead to trouble

  • #15
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Fill a table key_pool(id int) with all 99999 possibilities, and when you insert a new row to your other table grab an id from key_pool (order by rand() limit 1) and delete from the key_pool. You can do this all transparently from a trigger on insert. This method substitutes sudden catastrophe for the slow degradation of the collision method supra.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •