dongpan1416 2012-09-18 06:56
浏览 17
已采纳

有没有更好(更短)的方法来生成随机字符串? [关闭]

Any random number between 0 to 100 can be generated like this:

<?php
    $min = 0;
    $max = 100;
    $random =  rand($min,$max);
?>

Now I can place this $random anywhere in my file to generate a random number. But the problem is when I place this on more than one place all those places have same value (if one random number is 17 all will be 17).

I am doing something like this to do that in more than one place:

    $random2 =  rand($min,$max);
    $random3 =  rand($min,$max);
    $random4 =  rand($min,$max);
    $random5 =  rand($min,$max);

And placing those in all locations.

Doing so takes more of my space in my script. Is there any better way to do what I want?

  • 写回答

4条回答 默认 最新

  • dousha1831 2012-09-18 07:08
    关注

    Why not making a function?:

    function myRand(){
        $min = 0;
        $max = 100;
        return mt_rand($min, $max);
    }
    echo myRand();
    

    that way you have to call only the function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看