doujionggan9570 2017-09-20 16:12
浏览 43
已采纳

mt_rand PHP - 几行数

is it possible to customize mt_rand to a function which gives me a line of numbers without repeats.

For example mt_rand(1,5) should give me 4,2,3,1,5 or something like that.

  • 写回答

2条回答 默认 最新

  • douzhaobo6488 2017-09-20 16:25
    关注

    This achieves what you ask in your question:

    $results = range(1, 5);
    shuffle($results);
    


    For more complex needs, you can use Faker.

    For instance the randomElements() method, here picking 5 non-duplicate numbers in the 1-10 range:

    $faker = \Faker\Factory::create();
    
    $results = $faker->randomElements(range(1, 10), 5, false);
    

    (though, this can also be done with regular PHP easily:)

    $pool = range(1, 10);
    shuffle($pool);
    
    $results = array_slice($pool, 0, 5);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导