Bug #64167 [Opn->Nab]: Recursive function using mt_rand sometimes returns NULL
| From: | nikic@php.net | Date: | Thu, 07 Feb 2013 07:41:17 +0000 |
| Subject: | Bug #64167 [Opn->Nab]: Recursive function using mt_rand sometimes returns NULL | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-176501@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64167&edit=1
ID: 64167
Updated by: nikic@php.net
Reported by: martin at psinas dot com
Summary: Recursive function using mt_rand sometimes returns
NULL
-Status: Open
+Status: Not a bug
Type: Bug
Package: Variables related
Operating System: linux mint
PHP Version: 5.4.11
Block user comment: N
Private report: N
New Comment:
You are missing a return statement before the recursive mtrand_except call ;)
Corrected version: http://codepad.viper-7.com/I0zgc5
Previous Comments:
------------------------------------------------------------------------
[2013-02-07 02:45:12] martin at psinas dot com
Description:
------------
Variable is properly set but returns as NULL value
Test script:
---------------
<?php
function mtrand_except($min, $max, $except) {
$random_number = mt_rand($min, $max);
if(!in_array($random_number, $except)) {
echo $random_number.'<br />';
return $random_number;
}
mtrand_except($min, $max, $except);
}
$random_arr = array();
for($i=1; $i<=90; $i++) {
$rand = mtrand_except(1, 90, $random_arr);
echo $i.'<br />'.$rand.'<hr />';
$random_arr[] = $rand;
}
Expected result:
----------------
60
1
60
------
19
2
19
------
57
3
57
------
etc...
Actual result:
--------------
60
1
60
------
19
2
NULL
------
57
3
57
------
etc...
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64167&edit=1