php lcg value与mt rand生成0 1随机小数的效果比较

时间:2019-02-19
本文章向大家介绍php lcg value与mt rand生成0 1随机小数的效果比较,主要包括php lcg value与mt rand生成0 1随机小数的效果比较使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
                     

因工作需要使用php生成0~1随机小数,之前写过一篇《php生成0~1随机小数方法》,基于mt_rand()mt_getrandmax()实现。

后来有网友评论,php原生方法lcg_value()可实现0~1随机小数生成。

lcg_value说明

float lcg_value ( void )
  • 1

lcg_value() 返回范围为 (0, 1) 的一个伪随机数。本函数组合了周期为 2^31 - 85 和 2^31 - 249 的两个同余发生器。本函数的周期等于这两个素数的乘积。

返回:范围为 (0, 1) 的伪随机数。

<?phpfor($i=0; $i<5; $i++){    echo lcg_value().PHP_EOL;}?>
  • 1
  • 2
  • 3
  • 4
  • 5

输出:

0.115165158519950.0646845515752970.682751740311890.557307465290990.70215008878091
  • 1
  • 2
  • 3
  • 4
  • 5


两种生成0~1随机小数方法进行比较


1.执行时间比较

执行10万次基于mt_rand()与mt_getrandmax()算法的运行时间

<?php/** * 生成0~1随机小数 * @param  Int   $min * @param  Int   $max * @return Float */function randFloat($min=0, $max=1){    return $min + mt_rand()/mt_getrandmax() * ($max-$min);}// 获取microtimefunction get_microtime(){    list($usec, $sec) = explode(' ', microtime());    return (float)$usec + (float)$sec;}// 记录开始时间$starttime = get_microtime();// 执行10万次获取随机小数for($i=0; $i<100000; $i++){    randFloat();}// 记录结束时间$endtime = get_microtime();// 输出运行时间printf("run time %f ms\r\n", ($endtime-$starttime)*1000);?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

输出:run time 266.893148 ms


执行10万次lcg_value()的运行时间

<?php// 获取microtimefunction get_microtime(){    list($usec, $sec) = explode(' ', microtime());    return (float)$usec + (float)$sec;}// 记录开始时间$starttime = get_microtime();// 执行10万次获取随机小数for($i=0; $i<100000; $i++){    lcg_value();}// 记录结束时间$endtime = get_microtime();// 输出运行时间printf("run time %f ms\r\n", ($endtime-$starttime)*1000);?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

输出:run time 86.178064 ms

执行时间上比较,因为lcg_value()直接是php原生方法,而mt_rand()与mt_getrandmax()需要调用两个方法,并需要进行计算,因此lcg_value()的执行时间大约快3倍。


2.随机效果比较

基于mt_rand()与mt_getrandmax()算法的随机效果

<?php/** * 生成0~1随机小数 * @param  Int   $min * @param  Int   $max * @return Float */function randFloat($min=0, $max=1){    return $min + mt_rand()/mt_getrandmax() * ($max-$min);}header('content-type: image/png');$im = imagecreatetruecolor(512, 512);$color1 = imagecolorallocate($im, 255, 255, 255);$color2 = imagecolorallocate($im, 0, 0, 0);for($y=0; $y<512; $y++){    for($x=0; $x<512; $x++){        $rand = randFloat();        if(round($rand,2)>=0.5){            imagesetpixel($im, $x, $y, $color1);        }else{            imagesetpixel($im, $x, $y, $color2);        }    }}imagepng($im);imagedestroy($im);?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

随机效果图:



lcg_value()的随机效果

<?phpheader('content-type: image/png');$im = imagecreatetruecolor(512, 512);$color1 = imagecolorallocate($im, 255, 255, 255);$color2 = imagecolorallocate($im, 0, 0, 0);for($y=0; $y<512; $y++){    for($x=0; $x<512; $x++){        $rand = lcg_value();        if(round($rand,2)>=0.5){            imagesetpixel($im, $x, $y, $color1);        }else{            imagesetpixel($im, $x, $y, $color2);        }    }}imagepng($im);imagedestroy($im);?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

随机效果图:




随机效果上比较,可以看出使用mt_rand()与mt_getrandmax()算法生成的随机效果较乱序,随机效果比lcg_value()较好。


总结:lcg_value()执行速度快,但随机效果不及基于mt_rand()mt_getrandmax()算法实现。