功能不一致

时间:2019-01-03 15:57:26

标签: php arrays random

我是PHP新手。我有一个可以正常工作的函数(从函数内部打印到屏幕上的值恰好是我期望的值),但有时仅返回答案(其他时候返回NULL)。我相信我已经将错误隔离到涉及到我对PHP static功能的使用上了,但是我不确定自己到底在多大程度上改正/如何修复它。我试图通过创建一个新的非静态变量来存储结果来解决该问题,并将我的代码从始终返回NULL改进为有时仅返回NULL。错误函数是我编写的一大套程序的一部分,因此我将包括它以及用于检查其功能的测试功能。


probGen.php

<?
    require_once("randX.php");
    require_once("../displayArray.php");
    error_reporting(E_ERROR | E_WARNING | E_PARSE);

    function probGen(array $arr, float $control = 0.01) 
/*
*   Generates a valid, random probability distribution for a given array of elements, that can be used in conjunction with "probSelect()".
*   Input: 
        $arr: An array of elements.
        $control: A value that decides how much mass is allowed to be unilaterally dumped onto one element. A high value would permit distributions where most of the mass is concentrated on one element. 
        If an invalid value is provided, the default is used.
*   Output: An associative array where the keys are the elements in the original array, and the values are their probabilities. 
*/
    {
        $control = ($control <= 1 && $control >= 0)?($control):(0.01);  #Use the default value if an invalid number is supplied.
        static $result = [];    #Initialises $result with an empty array on first function call.
        static $max = 1;    #Initialises $max with 1 on first function call.
        foreach ($arr as $value) 
        {
            $x = randX(0, $max);    #Random probability value.
            $result[$value] = ($result[$value] + $x)??0;    #Initialise the array with 0 on first call, and on subsequent calls increment by $x to assign probability mass.
            $max -= $x;     #Ensures that the probability never sums to more than one.
        }
        print("<br>sum = ".array_sum($result)."<br><br>");
        displayArray($result);
/*
*   After the execution of the above code, there would be some leftover probability mass. 
*   The code below adds it to a random element.
*/
        $var = array_values($arr);
        if($max <= $control)    #To limit concentration of most of the probability mass in one variable.
        {
            $result[$var[rand(0,(count($var)-1))]] += $max; #Selects a random key and adds $max to it.
            displayArray($result);
            print("<br>sum = ".array_sum($result)."<br><br>");  
            $sol = $result;
            return $sol;
        }
        else
            probGen($arr, $control);    
    }
?>

test.php

<?

    /*
    *   This file contains some functions that can be used for assigning probabilities to an array of elements or selecting an element from said array with a given probability.
    */

    require_once("../displayArray.php");
    require_once("confirm.php");
    require_once("randX.php");
    require_once("probGen.php");
    require_once("probSelect.php");

    $test = ["California" => 0.37, "Florida" => 0.13, "New York" => 0.28, "Washington" => 0.22];
    $testX = array_keys($test);

    var_dump(probGen($testX));

    /*for ($i=0; $i < 100 ; $i++) 
    { 
        print(var_dump(confirm(probGen($testX))));
    }*/

?>

probGen()按预期工作的屏幕截图:
enter image description here

probGen()的屏幕截图失败:
enter image description here

从测试中,我可以确定如果递归多次发生,probGen()将失败。

1 个答案:

答案 0 :(得分:1)

作为 @aynber 的评论(我只写了他所有的话,所以这篇帖子会有答案)。

调用递归调用时,应添加return(在最后一行),以使probGen函数的返回值起泡为return probGen(); < / p>

通常来说,让PHP返回NULL有时可以用作提示,以返回非值。

也可以看到this个问题,相同的问题。