马黑PHP整站系统

      1
2345678
9101112131415
16171819202122
23242526272829
3031     

[复位日记]

最新评论

悄然
飞飞
马黑
悄然
飞飞

网站统计

·今日访问 : 41
·页面点击 : 42
·当前在线 : 2

2025年3月3日 星期一[阅读 59]

JS使用 set 对象生成指定范围内N个不重复的随机数:

  1. const ranUniqueNumbers = (total,max,min) => {
  2. //处理输出总数与大小范围的关系 :避免挂起
  3. if(total > max - min) total = max - min;
  4. const set = new Set();
  5. while (set.size < total) {
  6. set.add(Math.floor(Math.random() * (max - min + 1)) + min);
  7. }
  8. return Array.from(set);
  9. };

  10. //应用实例 :在0~100之间随机取出90个不重复的数
  11. console.log(ranUniqueNumbers(90, 100, 0));
所取到的随机数包含边界数。

2025': 前一则  下一则    

发表评论:

       

Copyright © 2023 All Right Reserved 马黑PHP文章管理整站系统v1.8
联系我们: gxblk@163.com