random

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

random

Post 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
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

Post 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
Post Reply