Page 1 of 1

random

Posted: Thu Sep 04, 2003 9:49 am
by junky
hi guys, ive a question
im trying to made a script with rand, but the priority in the rand is important. I explain myself more:
ive a selectbox with prioriter from 0 to 9 10 is the smallest priority, and 10 is the biggest priority.
in my case, 10 has 10 more chance of being choose then the 1.
this is how ive started:

<?php
$pri = rand(0,10);

$max = 0;

while ($max == 0) {
//$max = "SELECT COUNT(table_id)
FROM table_name
WHERE priority=$pri";
}

$choose = rand(0,$max);
$db_query = "SELECT *
FROM table_name
LIMIT $choose,1";


but with this code, 1 has the same chance of being choose then the 10, and id like respect the priority.
if someone can help me, it would be really appreciated. and without case (if possible).
thanks

Posted: Thu Sep 04, 2003 10:31 am
by junky
ive found a way:
$liste = array(1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9);
$nb = array_rand($liste);
print $liste[$nb];

i know its primitive, im gonna work on this, and post the new version (as a function) soon.
later