Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,345

    * Another (interesting) PHP Question!

    Lets say (dynamically) i have some numbers,

    lets say 2 , 3 6, 7

    I want to pick one of them randomly.. I can do it if the numbers are in order 0,1,2,3 ... I do not know how to do it if they are not ordered?

    anyone tried it before?

    Peace,

  2. #2
    Join Date
    Jan 2003
    Location
    Perth, WA, Australia
    Posts
    1,276
    Where are these numbers stored? In a variable (string), like "2367" or in an array?
    nu-metal.org :: coming soon

  3. #3
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,345
    Originally posted by digitok
    Where are these numbers stored? In a variable (string), like "2367" or in an array?
    They are stored in a variable , lets say $numbers like this:

    1|3|9 ..

    I can easily put them into an array if it will help!

  4. #4
    Join Date
    Aug 2003
    Location
    PA
    Posts
    110
    Go to this page at php.net. It covers a function called array_rand() and seeding.

    Hope it was what you were looking for!

  5. #5
    I think that this function can pick the same member of the array more than once.

  6. #6
    Join Date
    May 2002
    Location
    UK
    Posts
    2,997
    If they are stored one per entry you can use the ORDER BY RAND() function in MySQL.

  7. #7
    Join Date
    Aug 2003
    Location
    PA
    Posts
    110
    Although it can repick a number in consecutive fashion, the original poster only asked to pick at random.

    And if they are in a DB, you should just use a SQL, or proprietary SQL, statement to call a random number as Rich pointed out.

    You can also take your count of elements in the array, get a random, then do a modular calc on the number( % ), and use that random number as the array element.

    Here is how to do it without using the PHP functions.

    PHP Code:

    $choices 
    = array("2","3","6","7");

    $elem = (rand() % count($choices));

    $random_element $choices[$elem]; 
    I used a longer approach for this, so, you could, but don't have to, optimize it further. I also didn't do the seeding in this example, but that is rather easy as well.
    Later

  8. #8
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,345
    Originally posted by Rich2k
    If they are stored one per entry you can use the ORDER BY RAND() function in MySQL.
    Looks interesting so I can do this:

    SELECT * FROM banners Where `group`='general' ORDER BY RAND()

    let me try that ( i will also look it up)..

    sounds easier than extracting the numbers, choose one and the get it...


    thnx alot guys for ur help

  9. #9
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,345
    I did this:

    it picks the same number each time?

    PHP Code:
    $query_view_check "SELECT * FROM stats WHERE `group`='$group' ORDER BY RAND() LIMIT 1";
    $result_view_check = @mysql_query($query_view_check);
    $num_view_check = @mysql_fetch_array($result_view_check);
    echo 
    $num_view_check[0]; 

  10. #10
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,345
    Cool...

    it didnt work for some reaon. its working now..

    thnx guys

Posting Permissions

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