Jump to content

[SOLVED] How to randomly select a number from a discontinuous set of numbers ?


tmyonline

Recommended Posts

Hi guys:

 

Here's my problem:

 

    $expSQL    = "SELECT experiences_key FROM experiences_teachers_jn WHERE schools_key = $s";

    $expResult  = mysql_db_query($db,$expSQL,$cid);

    $expRow    = @mysql_fetch_assoc($expResult);

 

Because there are many "experiences_key" associated with a given schools_key (many-to-one relationship), after fetching the $expResult, the $expRow is a column of numbers.  I would like to randomly select a number from this list (column).  However, because these numbers do not form a continuous sequence, if I specify the lower bound and upper bound, there will be chances that the number selected is not on the list.  For example, $expRow could be {1, 5, 12, 21,..., 783}.

 

I was thinking of storing these values in an array and go from there but is there a better way to do this ?

 

Thanks.

Tommy

I'd just add an order by RAND() clause on the end of your SQL statment:

 

$expSQL     = "SELECT experiences_key FROM experiences_teachers_jn WHERE schools_key = $s ORDER BY RAND() LIMIT 1";   

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.