I have a MySql table “regalos” with the following structure:
id name stock
1 Type A 10
2 Type B 5
3 Type C 1
etc etc etc
I am taking a random id based on the available id’s with the following query:
<?php
$regalo_sql = "SELECT * FROM `regalos` WHERE id NOT IN (32) AND `stock` > 0 ORDER BY RAND() LIMIT 1";
$regalo_result = $link -> query($regalo_sql) or die ('Unable to execute query. '. mysqli_error($link));
// echo $regalo_sql;
if(mysqli_num_rows($regalo_result) > 0){
foreach ($regalo_result as $row){
$t = $row['id'];
}
}else{
echo 'There are no items that meet';
}
?>
I need to choose a gift id randomly. But this randomness should not be 33.33%, as ORDER BY RAND() does. But rather, depending on the stock, each gift should have a weight.
In this case, the randomness of each type of gift would be as follows: gift type A = 62.5%, gift type B = 31.25% and gift type C = 6.25%.
But I need to automate this so that it varies according to the quantity of each gift.
I have asked gemini (AI) but have not come up with any solid results.
I have no base knoweledge about maths, stadistics o mysql queries. I’ve been learning about the field, solving problems.
fon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.