Edit report at https://bugs.php.net/bug.php?id=64167&edit=1

 ID:                 64167
 Updated by:         ni...@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

Reply via email to