Choose random value by php weight

I am going to create a "Lottary System".

Take a look at my table:

userid-lottaryid-amount
1 -------- 1 ----  1
2 -------- 1 ---- 10
3 -------- 1 ---- 15
4 -------- 1 ---- 20

I want to choose a winner. and the other person in second place.

I just canโ€™t choose a winner by chance, because the 4th user has 20 tickets, and the 1st user has only one. Therefore, I need to generate random weight results in order to be more fair.

I found the php function below, but I could not figure out how to use it.

      function weighted_random_simple($values, $weights){ 
      $count = count($values); 
      $i = 0; 
      $n = 0; 
      $num = mt_rand(0, array_sum($weights)); 

      while($i < $count){
          $n += $weights[$i]; 
          if($n >= $num){
              break; 
          }
          $i++; 
      } 
      return $values[$i]; 

  }

    $values = array('1', '10', '20', '100');
    $weights = array(1, 10, 20, 100);

    echo weighted_random_simple($values, $weights);

I have to get useridcolomn as an array before $valuesand amountcolomn before $weights. But I can not.

Here is my code:

    $query = $handler->prepare("SELECT 

      `cvu`.`lottaryid` as `lottaryid`, 
      `cvu`.`userid` as `userid`, 
      `cvu`.`amount` as `amount`, 

      `members`.`id` as `members_memberid`, 
      `members`.`username` as `username`

      FROM `lottariesandmembers` as `cvu`

      LEFT JOIN `members` as `members` ON `cvu`.`userid` = `members`.`id`  WHERE `cvu`.`lottaryid` = 2");
    $query->bindParam(':lottaryid', $lottaryid, PDO::PARAM_INT);
    $query->execute();



    while($r = $query->fetch()) {

        for ( $count=1 ; $count <= $r["amount"] ; $count++ ) {

            $abcprint = "$r[userid].$count - $r[username] - <br>";

            echo "$abcprint";

        }


    } 

This code, which I have, only lists users as many times as their total. For example:

1.1 user1
2.1 user2
2.2 user2
2.3 user2
..
2.10 user2
3.1 user3
..
3.15 user3
4.1 user4
..
4.20 user4

etc. But I'm stuck on how to choose a winner on this list.

script, .

, -.

+4
4

, , , , .

while($r = $query->fetch()) {
    for ( $i=0; $i <= $r["amount"]; $i++ ) {
        // Add the user into the array as many times as they have tickets
        $tickets[] = $r['userid'];
    }
}

// select the first place winner
$first = $tickets[mt_rand(0, count($tickets) - 1)];

// remove the first place winner from the array
$tickets = array_values(array_filter($tickets, function($x) use ($first) { 
    return $x != $first; 
}));

// select the second place winner
$second = $tickets[mt_rand(0, count($tickets) - 1)];

, , ...

+2

, .

.

, . "", . .. "a", 20 "b" .....

<?php

$holder_totals = array(
    'a' => '10',
    'b' => '20',
    'c' => '20',
    'd' => '50'
);

$big_hat = array();
foreach($holder_totals as $holder_id => $total) {
    $holder_hat = array_fill(0, intval($total), $holder_id);
    $big_hat    = array_merge($big_hat, $holder_hat);
}

// Drum roll
foreach (range(1,4) as $n) {
    $random_key = array_rand($big_hat);
    printf("Winner %d is %s.\n", $n, $big_hat[$random_key]);
    unset($big_hat[$random_key]); // Remove winning slip
}

:

Winner 1 is d.
Winner 2 is c.
Winner 3 is d.
Winner 4 is b.

:

Array
(
    [0] => a
    [1] => a
    [2] => a
    [3] => a
    [4] => a
    [5] => a
    [6] => a
    [7] => a
    [8] => a
    [9] => a
    [10] => b
    [11] => b
    [12] => b
    [13] => b
    [14] => b
    ... and so on...
)
+2
  /**
   * getRandomWeightedElement()
   * Utility function for getting random values with weighting.
   * Pass in an associative array, such as array('A'=>5, 'B'=>45, 'C'=>50)
   * An array like this means that "A" has a 5% chance of being selected, "B" 45%, and "C" 50%.
   * The return value is the array key, A, B, or C in this case.  Note that the values assigned
   * do not have to be percentages.  The values are simply relative to each other.  If one value
   * weight was 2, and the other weight of 1, the value with the weight of 2 has about a 66%
   * chance of being selected.  Also note that weights should be integers.
   * 
   * @param array $weightedValues
   */
  function getRandomWeightedElement(array $weightedValues) {
    $rand = mt_rand(1, (int) array_sum($weightedValues));

    foreach ($weightedValues as $key => $value) {
      $rand -= $value;
      if ($rand <= 0) {
        return $key;
      }
    }
  }

Here is an efficient and flexible feature. But you have to change it if you want to use non-integer weighting.

+2
source

You can use the weightedChoice function from my nspl library .

use function \nspl\rnd\weightedChoice;

// building your query here

$pairs = [];
while($r = $query->fetch()) {
    $pairs[] = [$r['userid'], $r['amount']];
}

$winnerId = weightedChoice($pairs);

You can install the library with the composer:

composer require ihor/nspl

Or you can simply reuse the weightedChoice code from GitHub:

/**
 * Returns a random element from a non-empty sequence of items with associated weights
 *
 * @param array $weightPairs List of pairs [[item, weight], ...]
 * @return mixed
 */
function weightedChoice(array $weightPairs)
{
    if (!$weightPairs) {
        throw new \InvalidArgumentException('Weight pairs are empty');
    }

    $total = array_reduce($weightPairs, function($sum, $v) { return $sum + $v[1]; });
    $r = mt_rand(1, $total);

    reset($weightPairs);
    $acc = current($weightPairs)[1];
    while ($acc < $r && next($weightPairs)) {
        $acc += current($weightPairs)[1];
    }

    return current($weightPairs)[0];
}
+1
source

Source: https://habr.com/ru/post/1620613/


All Articles