Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 试图得到一个随机数,并有机会得到那个数_Php_Mysql_Random - Fatal编程技术网

Php 试图得到一个随机数,并有机会得到那个数

Php 试图得到一个随机数,并有机会得到那个数,php,mysql,random,Php,Mysql,Random,我试图创造这个,但它没有真正的工作 这是我希望它如何工作的表格。。 这是我的数据库的图片(phpmyadmin) 我想让它先看一下百分比,然后找到要使用的百分比 IE:75%的几率获得1到50之间的随机数。随机数介于51和200之间的概率为20%,以此类推 这是我的密码: if ($_GET['method'] == "test") { $sql = "SELECT 1percentage, 1min, 1max, 2percentage, 2min, 2max, 3percenta

我试图创造这个,但它没有真正的工作

这是我希望它如何工作的表格。。

这是我的数据库的图片(phpmyadmin)

我想让它先看一下百分比,然后找到要使用的百分比

IE:75%的几率获得1到50之间的随机数。随机数介于51和200之间的概率为20%,以此类推


这是我的密码:

if ($_GET['method'] == "test") {
    $sql = "SELECT 1percentage, 1min, 1max, 2percentage, 2min, 2max, 3percentage, 3min, 3max, 4percentage, 4min, 4max FROM `keys`";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) {
            $total = $row["1percentage"] + $row["2percentage"] + $row["3percentage"] + $row["4percentage"] + 1;
            $random = rand(0, $total);
            if ($random < $row['1percentage'] || $random == $row['1percentage']) {
                $amount = rand($row['1min'], $row['1max']);
            } elseif ($random > $row['1percentage'] && $random < $row['2percentage'] || $random == $row['2percentage']) {
                $amount = rand($row['2min'], $row['2max']);
            } elseif ($random > $row['2percentage'] && $random < $row['3percentage'] || $random == $row['3percentage']) {
                $amount = rand($row['3min'], $row['3max']);
            } elseif ($random > $row['4percentage'] || $random == $row['4percentage']) {
                $amount = rand($row['4min'], $row['4max']);
            } else {
                exit("0");
            }

            echo $amount;
        }
}
if($\u GET['method']=“test”){
$sql=“从“键”中选择1%的、1分钟的、1最大的、2%的、2分钟的、2最大的、3%的、3分钟的、3最大的、4%的、4分钟的、4最大的;
$result=$conn->query($sql);
而($row=$result->fetch_assoc()){
$total=$row[“1百分比”]+$row[“2百分比”]+$row[“3百分比”]+$row[“4百分比”]+1;
$random=rand(0,$total);
如果($random<$row['1percentage']| |$random==$row['1percentage'])){
$amount=rand($row['1min'],$row['1max']);
}elseif($random>$row['1percentage']和&$random<$row['2percentage']| |$random==$row['2percentage'])){
$amount=rand($row['2min'],$row['2max']);
}elseif($random>$row['2percentage']和&$random<$row['3percentage']| |$random==$row['3percentage'])){
$amount=rand($row['3min'],$row['3max']);
}elseif($random>$row['4percentage']| |$random==$row['4percentage'])){
$amount=rand($row['4min'],$row['4max']);
}否则{
退出(“0”);
}
echo美元金额;
}
}
但它的输出是或1到50,或1001到10000


那么我做错了什么?

查看您提供的代码和数据库记录中的值,您当前的设置将无法运行

首先,应按如下方式清理
if
语句:

if ($_GET['method'] == "test") {
$sql = "SELECT 1percentage, 1min, 1max, 2percentage, 2min, 2max, 3percentage, 3min, 3max, 4percentage, 4min, 4max FROM `keys`";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
        $total = $row['1percentage'] + $row['2percentage'] + $row['3percentage'] + $row['4percentage'] + 1;
        $random = rand(0, $total);
        if ($random <= $row['1percentage']) {
            $amount = rand($row['1min'], $row['1max']);
        } elseif ($random > $row['1percentage'] && $random <= $row['2percentage']) {
            $amount = rand($row['2min'], $row['2max']);
        } elseif ($random > $row['2percentage'] && $random <= $row['3percentage']) {
            $amount = rand($row['3min'], $row['3max']);
        } elseif ($random >= $row['4percentage']) {
            $amount = rand($row['4min'], $row['4max']);
        } else {
            exit("0");
        }

        echo $amount;
    }

}

在这个问题上花了一些时间之后,我想出了一个稍微不同的解决方案。本例中概率的实现是不好的,但它确实展示了为几乎任何数量的生成数据获得所需概率的基本示例。如果将
$how\u numbers\u to\u generate
变量设置为小于50,则下面的代码将无法正常工作,但这是概率的方式。数据集必须相当大,才能获得某种程度上准确的概率。此外,为了使示例起作用,您必须在
$mysqli=newmysqli(“本地主机”、“我的名字”、“我的通行证”、“我的数据库”)中替换用户名、密码和数据库与您使用的代码。完整代码如下

<?php

$mysqli = new mysqli("localhost", "my_username", "my_password", "my_database");

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$how_much_numbers_to_generate = 1000;

$result = $mysqli->query("SELECT 1percentage, 1min, 1max, 2percentage, 2min, 2max, 3percentage, 3min, 3max, 4percentage, 4min, 4max FROM percentages");

while($row = $result->fetch_assoc())
{
    $first_percentage = $row['1percentage'];
    $first_min = $row['1min'];
    $first_max = $row['1max'];
    $second_percentage = $row['2percentage'];
    $second_min = $row['2min'];
    $second_max = $row['2max'];
    $third_percentage = $row['3percentage'];
    $third_min = $row['3min'];
    $third_max = $row['3max'];
    $fourth_percentage = $row['4percentage'];
    $fourth_min = $row['4min'];
    $fourth_max = $row['4max'];

    $first_numbers_generated=0;
    $second_numbers_generated=0;
    $third_numbers_generated=0;
    $fourth_numbers_generated=0;

    $random_array = array();

    for($numbers_generated=0; $numbers_generated < $how_much_numbers_to_generate; $numbers_generated++)
    {
        if( $first_numbers_generated==0 || $first_numbers_generated/$numbers_generated * 100.0 <=  $first_percentage)
        {
            $random_value = rand($first_min, $first_max);
            $first_numbers_generated++;
        }

        else if( $second_numbers_generated==0 || $second_numbers_generated/$numbers_generated * 100.0 <=  $second_percentage)
        {
            $random_value = rand($second_min, $second_max);
            $second_numbers_generated++;
        }

        else if( $third_numbers_generated==0 || $third_numbers_generated/$numbers_generated * 100.0 <=  $third_percentage)
        {
            $random_value = rand($third_min, $third_max);
            $third_numbers_generated++;
        }

        else if( $fourth_numbers_generated==0 || $fourth_numbers_generated/$numbers_generated * 100.0 <=  $fourth_percentage)
        {
            $random_value = rand($fourth_min, $fourth_max);
            $fourth_numbers_generated++;
        }
        else
        {
            $random_value = rand($first_min, $fourth_max);
            if ($random_value <= $first_max) 
            {
                $first_numbers_generated++;
            }
            else if ($random_value <= $second_max)
            {
                $second_numbers_generated++;
            }
            else if ($random_value <= $third_max)
            {
                $third_numbers_generated++;
            }
            else
            {
                $fourth_numbers_generated++;
            }
        }

        array_push($random_array, $random_value);
    }
    echo "First numbers generated: " . $first_numbers_generated . "<br/>";
    echo "Second numbers generated: " . $second_numbers_generated . "<br/>";
    echo "Third numbers generated: " . $third_numbers_generated . "<br/>";
    echo "Fourth numbers generated: " . $fourth_numbers_generated . "<br/>";
    echo "First probability: " . ($first_numbers_generated/$numbers_generated * 100.0) . "<br/>";
    echo "Second probability: " . ($second_numbers_generated/$numbers_generated * 100.0) . "<br/>";
    echo "Third probability: " . ($third_numbers_generated/$numbers_generated * 100.0) . "<br/>";
    echo "Fourth probability: " . ($fourth_numbers_generated/$numbers_generated * 100.0) . "<br/>";
}

这是一个有趣的问题,我认为它以前已经解决了

Python的
random.choices
函数解决了与此类似的问题

下面是一个在PHP中转换它以适应当前用例的尝试

# https://github.com/python/cpython/blob/master/Lib/bisect.py#L22
function bisect(array $a, float $x, int $lo, int $hi): int {
    $mid = $lo + $hi;
    while ($lo < $hi) {
        $mid = ($lo + $hi) / 2;
        if ($x < $a[$mid]) {
            $hi = $mid;
        } else {
            $lo = $mid + 1;
        }
    }
    return $lo;
}

/*
 * Generate a random number 
 * from 1 to 50, with the generation probability 75%,
 * from 51 to 200 with the generation probability 20%,
 * from 201 to 1000 with the generation probability 4% and 
 * from 1001 to 10000 with the generation probability 1%.
 */
function randomChoice(array $population, array $weights): int
{
    $cummWeights = array_reduce($weights, function ($acc, $curr) {
        $lastSlice = array_slice($acc, -1);
        $acc[] = $lastSlice[0] + $curr;
        return $acc;
    }, [0]);

    # remove 0 starting value
    array_shift($cummWeights);

    $lastWeightSlice = array_slice($cummWeights, -1);
    $total = $lastWeightSlice[0];

    $probability = ((float)rand() / (float)getrandmax());
    $needle = $probability * $total;

    # look for $needle in $cummWeights, then use that to select which population distribution to generate a random number from.
    # https://github.com/python/cpython/blob/master/Lib/random.py#L387
    $index = bisect($cummWeights, $needle, 0, count($population));


    $distribution = $population[$index];
    return rand($distribution[0], $distribution[1]);
}

echo randomChoice(
    [
        [51, 200], 
        [1, 50], 
        [201, 1000], 
        [1001, 10000]
    ],
    [20, 75, 4, 1]
);
#https://github.com/python/cpython/blob/master/Lib/bisect.py#L22
函数对分(数组$a,浮点$x,整数$lo,整数$hi):整数{
$mid=$lo+$hi;
而($lo<$hi){
$mid=($lo+$hi)/2;
如果($x<$a[$mid]){
$hi=$mid;
}否则{
$lo=$mid+1;
}
}
返回$lo;
}
/*
*生成一个随机数
*从1到50,生成概率为75%,
*从51到200,生成概率为20%,
*从201到1000,生成概率为4%,并且
*从1001到10000,生成概率为1%。
*/
函数随机选择(数组$population,数组$weights):int
{
$cummWeights=array\u reduce($weights,function($acc,$curr){
$lastSlice=数组×切片($acc,-1);
$acc[]=$lastSlice[0]+$curr;
返回$acc;
}, [0]);
#删除0起始值
阵列移动(单位:立方米);
$lastWeightSlice=array\u slice($cummWeights,-1);
$total=$lastWeightSlice[0];
$probability=((float)rand()/(float)getrandmax());
$pinder=$probability*$total;
#在$cummWeights中查找$needle,然后使用它选择要从中生成随机数的人口分布。
# https://github.com/python/cpython/blob/master/Lib/random.py#L387
$index=bisect($cummWeights,$needle,0,count($population));
$distribution=$population[$index];
返回兰特($distribution[0],$distribution[1]);
}
回声随机选择(
[
[51, 200], 
[1, 50], 
[201, 1000], 
[1001, 10000]
],
[20, 75, 4, 1]
);

我会使用
$随机查看:@EugeneAnisiutkin我更改了它,但它仍然提供相同的输出。@草莓我发送的表格截图是从数据库中使用
选择的。
没有错误。@youtube只是另一个-截图不是MCVE,你真的应该创建一个。另外,屏幕截图不是SQL查询的结果,根据屏幕截图判断,SQL查询失败。它仍然无法按预期工作,算法本身是错误的<代码>$total=$row['1percentage']+$row['2percentage']+$row['3percentage']+$row['4percentage']+1
给出101和
$random=rand(0,$total)
给出了一个介于0和101之间的随机值,该值与percetages无关。@EugeneAnisiutkin,我同意,如果不知道他们试图实现什么,当前的算法就没有意义。我的回答只是试图解释为什么他们在任何情况下都只能看到两个条件中的一个得到满足。一旦解决了最初的问题,希望它能满足他们的特殊目的,或者他们能更容易地修改算法。我很清楚他们想要实现什么。任务是生成1到50的随机数,生成概率为75%,51到200的随机数,生成概率为20%,201到1000的随机数,生成概率为4%,1001到10000的随机数,生成概率为1%。这不是一项简单的任务,因为概率部分只会
<?php

$mysqli = new mysqli("localhost", "my_username", "my_password", "my_database");

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$how_much_numbers_to_generate = 1000;

$result = $mysqli->query("SELECT 1percentage, 1min, 1max, 2percentage, 2min, 2max, 3percentage, 3min, 3max, 4percentage, 4min, 4max FROM percentages");

while($row = $result->fetch_assoc())
{
    $first_percentage = $row['1percentage'];
    $first_min = $row['1min'];
    $first_max = $row['1max'];
    $second_percentage = $row['2percentage'];
    $second_min = $row['2min'];
    $second_max = $row['2max'];
    $third_percentage = $row['3percentage'];
    $third_min = $row['3min'];
    $third_max = $row['3max'];
    $fourth_percentage = $row['4percentage'];
    $fourth_min = $row['4min'];
    $fourth_max = $row['4max'];

    $first_numbers_generated=0;
    $second_numbers_generated=0;
    $third_numbers_generated=0;
    $fourth_numbers_generated=0;

    $random_array = array();

    for($numbers_generated=0; $numbers_generated < $how_much_numbers_to_generate; $numbers_generated++)
    {
        if( $first_numbers_generated==0 || $first_numbers_generated/$numbers_generated * 100.0 <=  $first_percentage)
        {
            $random_value = rand($first_min, $first_max);
            $first_numbers_generated++;
        }

        else if( $second_numbers_generated==0 || $second_numbers_generated/$numbers_generated * 100.0 <=  $second_percentage)
        {
            $random_value = rand($second_min, $second_max);
            $second_numbers_generated++;
        }

        else if( $third_numbers_generated==0 || $third_numbers_generated/$numbers_generated * 100.0 <=  $third_percentage)
        {
            $random_value = rand($third_min, $third_max);
            $third_numbers_generated++;
        }

        else if( $fourth_numbers_generated==0 || $fourth_numbers_generated/$numbers_generated * 100.0 <=  $fourth_percentage)
        {
            $random_value = rand($fourth_min, $fourth_max);
            $fourth_numbers_generated++;
        }
        else
        {
            $random_value = rand($first_min, $fourth_max);
            if ($random_value <= $first_max) 
            {
                $first_numbers_generated++;
            }
            else if ($random_value <= $second_max)
            {
                $second_numbers_generated++;
            }
            else if ($random_value <= $third_max)
            {
                $third_numbers_generated++;
            }
            else
            {
                $fourth_numbers_generated++;
            }
        }

        array_push($random_array, $random_value);
    }
    echo "First numbers generated: " . $first_numbers_generated . "<br/>";
    echo "Second numbers generated: " . $second_numbers_generated . "<br/>";
    echo "Third numbers generated: " . $third_numbers_generated . "<br/>";
    echo "Fourth numbers generated: " . $fourth_numbers_generated . "<br/>";
    echo "First probability: " . ($first_numbers_generated/$numbers_generated * 100.0) . "<br/>";
    echo "Second probability: " . ($second_numbers_generated/$numbers_generated * 100.0) . "<br/>";
    echo "Third probability: " . ($third_numbers_generated/$numbers_generated * 100.0) . "<br/>";
    echo "Fourth probability: " . ($fourth_numbers_generated/$numbers_generated * 100.0) . "<br/>";
}
# https://github.com/python/cpython/blob/master/Lib/bisect.py#L22
function bisect(array $a, float $x, int $lo, int $hi): int {
    $mid = $lo + $hi;
    while ($lo < $hi) {
        $mid = ($lo + $hi) / 2;
        if ($x < $a[$mid]) {
            $hi = $mid;
        } else {
            $lo = $mid + 1;
        }
    }
    return $lo;
}

/*
 * Generate a random number 
 * from 1 to 50, with the generation probability 75%,
 * from 51 to 200 with the generation probability 20%,
 * from 201 to 1000 with the generation probability 4% and 
 * from 1001 to 10000 with the generation probability 1%.
 */
function randomChoice(array $population, array $weights): int
{
    $cummWeights = array_reduce($weights, function ($acc, $curr) {
        $lastSlice = array_slice($acc, -1);
        $acc[] = $lastSlice[0] + $curr;
        return $acc;
    }, [0]);

    # remove 0 starting value
    array_shift($cummWeights);

    $lastWeightSlice = array_slice($cummWeights, -1);
    $total = $lastWeightSlice[0];

    $probability = ((float)rand() / (float)getrandmax());
    $needle = $probability * $total;

    # look for $needle in $cummWeights, then use that to select which population distribution to generate a random number from.
    # https://github.com/python/cpython/blob/master/Lib/random.py#L387
    $index = bisect($cummWeights, $needle, 0, count($population));


    $distribution = $population[$index];
    return rand($distribution[0], $distribution[1]);
}

echo randomChoice(
    [
        [51, 200], 
        [1, 50], 
        [201, 1000], 
        [1001, 10000]
    ],
    [20, 75, 4, 1]
);