Hello! I have set up a script form called " form tools " without a problem, This script gathers all information in the registration form and stores it into a database (php and mysql), Now I would like to randomly choose 5 people from the data I collected from the database randomly, Since it’s a game, I would like it to be as random as possible, how do I do this?

Dani AI

Generated

Both and offered quick, workable ideas for picking winners. Those are fine for small sample sizes, but a production game-show draw needs attention to three things they did not fully cover: true uniformity and unpredictability, avoiding duplicates, and performance on large tables. The following alternatives and cautions fill those gaps.

Reservoir sampling is a simple, memory-light way to get k uniformly random rows in one streaming pass (no ORDER BY, no full result buffering). It works well when you can iterate rows from the database and want each row to have equal chance without loading all rows into PHP at once.

$k = 5;
$reservoir = [];
$i = 0;
$stmt = $pdo->query('SELECT id, name FROM contestants'); // stream rows from DB
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $i++;
    if ($i <= $k) {
        $reservoir[$i-1] = $row;
    } else {
        $r = random_int(1, $i);           // secure RNG
        if ($r <= $k) {
            $reservoir[$r-1] = $row;
        }
    }
}
// $reservoir now contains k distinct random rows

For very large tables with an indexed integer id, a faster approach is: get MIN(id)/MAX(id), generate secure random ids in that range, and fetch the next existing row with id >= rand_id LIMIT 1 (wrap to MIN if none). This avoids full-table sorting but requires handling gaps and re-tries to guarantee uniqueness.

Practical notes: use PDO or mysqli (avoid deprecated mysql_*), use random_int for unpredictability, track selected IDs to avoid duplicates, log the drawn IDs (and RNG method/seed) for auditability, and test on a copy of real-size data to validate performance. If legal/regulatory fairness matters, consider a certified RNG or third-party audit.

Recommended Answers

All 3 Replies

If you have a counter or id field in your table which is unique, you can do this easily.

<?php
$connection=mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
$query="select * from tablename";
$result=mysql_query($query);
$max=mysql_num_rows($result);//count the total records in the table
$random_members=array();
while(count($random_members) <= 5){ // until 5 members are selected
	$random=rand(1,$max);//generate random number
	if(! in_array($random,$random_members)){ //if random number is not in the array, add it. 
	 	$random_members[]=$random;
 	}
}
print_r($random_members); //print random numbers
?>

Thats it.. Cheers.. :)

If you have a counter or id field in your table which is unique, you can do this easily.

<?php
...... 
$query="select * from tablename";
$result=mysql_query($query);
..........
?>

There's a query that does this for you .. :)

select * from tablename order by rand() limit 5

There's a query that does this for you .. :)

select * from tablename order by rand() limit 5

I didn't know that ! thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.