• php lcg_value与mt_rand生成0~1随机小数的效果比较


    lcg_value说明

    float lcg_value ( void )
    • 1

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

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

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

    输出:

    1. 0.11516515851995
    2. 0.064684551575297
    3. 0.68275174031189
    4. 0.55730746529099
    5. 0.70215008878091
    • 1

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

    1.执行时间比较

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

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

    输出:run time 266.893148 ms


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

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

    输出:run time 86.178064 ms

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

    2.随机效果比较

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

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

    随机效果图:



    lcg_value()的随机效果

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

    随机效果图:




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


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

  • 相关阅读:
    图论基础(python蓝桥杯)
    语法基础(字符串)
    软件工程方法论
    Django、Flask和FastAPI的优缺点与适用场景
    Autosar Configuration(七) Security之导入DBC后配置SecOC安全报文
    Python新特性
    item_review-获得商品评论
    nuxt 如何生成sitemap.xml 动静态站点地图
    跨平台游戏引擎 Axmol-2.1.3 发布
    【JAVA基础】String类常用API
  • 原文地址:https://blog.csdn.net/liuliuhelingdao/article/details/127547870