【发布时间】:2019-05-30 16:02:01
【问题描述】:
this question 的答案说unset() 不起作用,但不清楚是什么。我有一个递归函数,它使用静态变量,但是在递归完成并返回值后,我需要重置这些变量或后续调用(当前在循环中调用递归函数)将返回错误值。
在链接的问题中,有人建议在我所做的功能之外尝试$var = NULL,但似乎没有效果。
我使用静态变量而不只是将它们写为函数的参数的原因是我不希望用户可以将参数传递给该函数的情况,因为函数的唯一参数应该由它内部提供。
我的代码
<?
require_once("randX.php"); #"randX()" generates a random floating point number in a specified range.
// require_once("../displayArray.php");
error_reporting(E_ERROR | E_WARNING | E_PARSE);
/*
* 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.
* @param array $arr: An array of elements for which the probability distribution would be generated.
* @param float $control: A variable which limits the inequality of the probability distribution.
*/
function probGen(array $arr, float $control = 0.01)
{
$control = ($control <= 1 && $control >= 0)?($control):(0.00001); #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.
}
/*
* 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.
// print("<br>Sum: ".array_sum($result)."<br>");
return $result;
}
else
{
return probGen($arr, $control);
}
}
$max = NULL;
unset($max);
$result = NULL;
unset($result);
?>
【问题讨论】: