In PHP, how do I generate a big pseudo-random number?

前端 未结 14 1211
遇见更好的自我
遇见更好的自我 2020-12-03 14:12

I\'m looking for a way to generate a big random number with PHP, something like:

mt_rand($lower, $upper);

The closer I\'ve

14条回答
  •  Happy的楠姐
    2020-12-03 14:56

    Generating 'n' random chars is not really an option, as random('9999999999') could still, theoretically, return 1...

    Here's a quite simple function:

    function bcrand($max) { 
        return bcmul($max, (string)mt_rand() / mt_getrandmax() ); 
    }
    

    Note that it will NOT return N bits of randomness, just adjust scale

提交回复
热议问题