Generating random results by weight in PHP?

2018-12-31 10:28发布

I know how to generate a random number in PHP but lets say I want a random number between 1-10 but I want more 3,4,5's then 8,9,10's. How is this possible? I would post what I have tried but honestly, I don't even know where to start.

标签: php random
12条回答
心情的温度
2楼-- · 2018-12-31 10:39

This tutorial walks you through it, in PHP, with multiple cut and paste solutions. Note that this routine is slightly modified from what you'll find on that page, as a result of the comment below.

A function taken from the post:

/**
 * weighted_random_simple()
 * Pick a random item based on weights.
 *
 * @param array $values Array of elements to choose from 
 * @param array $weights An array of weights. Weight must be a positive number.
 * @return mixed Selected element.
 */

function weighted_random_simple($values, $weights){ 
    $count = count($values); 
    $i = 0; 
    $n = 0; 
    $num = mt_rand(1, array_sum($weights)); 
    while($i < $count){
        $n += $weights[$i]; 
        if($n >= $num){
            break; 
        }
        $i++; 
    } 
    return $values[$i]; 
}
查看更多
查无此人
3楼-- · 2018-12-31 10:48

function getBucketFromWeights($values) { $total = $currentTotal = $bucket = 0;

foreach ($values as $amount) {
    $total += $amount;
}

$rand = mt_rand(0, $total-1);

foreach ($values as $amount) {
    $currentTotal += $amount;

    if ($rand => $currentTotal) {
        $bucket++;
    }
    else {
        break;
    }
}

return $bucket;

}

I ugh modified this from an answer here Picking random element by user defined weights

After I wrote this I saw someone else had an even more elegant answer. He he he he.

查看更多
人气声优
4楼-- · 2018-12-31 10:49
/**
 * @param array $weightedValues
 * @return string
 */
function getRandomWeightedElement(array $weightedValues)
{
    $array = array();

    foreach ($weightedValues as $key => $weight) {
        $array = array_merge(array_fill(0, $weight, $key), $array);
    }

    return $array[array_rand($array)];
}

getRandomWeightedElement(array('A'=>10, 'B'=>90));

This is very easy method. How get random weighted element. I fill array variable $key. I get $key to array $weight x. After that, use array_rand to array. And I have random value ;).

查看更多
心情的温度
5楼-- · 2018-12-31 10:50

For an efficient random number skewed consistently towards one end of the scale:

  • Choose a continuous random number between 0..1
  • Raise to a power γ, to bias it. 1 is unweighted, lower gives more of the higher numbers and vice versa
  • Scale to desired range and round to integer

eg. in PHP (untested):

function weightedrand($min, $max, $gamma) {
    $offset= $max-$min+1;
    return floor($min+pow(lcg_value(), $gamma)*$offset);
}
echo(weightedrand(1, 10, 1.5));
查看更多
步步皆殇っ
6楼-- · 2018-12-31 10:51

Since I used IainMH's solution, I may as well share my PHP code:

<pre><?php

// Set total number of iterations
$total = 1716;

// Set array of random number
$arr = array(1, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5);
$arr2 = array(0, 0, 1, 1, 2, 2, 2, 3, 3, 4, 5);

// Print out random numbers
for ($i=0; $i<$total; $i++){

    // Pick random array index
    $rand = array_rand($arr);
    $rand2 = array_rand($arr2);

    // Print array values
    print $arr[$rand] . "\t" . $arr2[$rand2] . "\r\n";

}

?></pre>
查看更多
只靠听说
7楼-- · 2018-12-31 10:53

There's a pretty good tutorial for you.

Basically:

  1. Sum the weights of all the numbers.
  2. Pick a random number less than that
  3. subtract the weights in order until the result is negative and return that number if it is.
查看更多
登录 后发表回答