生成 n 个不同的随机数且随机数区间为 [0,n)

Java 实现

import java.util.ArrayList;
import java.util.List;
import java.util.Random; /**
* TestRandom class
*
* @author libin
* @date 2019/4/17 9:40
*/
public class TestRandom {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(getDiffNO(10));
}
} /**
* 生成 n 个不同的随机数,且随机数区间为 [0,n)
*
* @param n 生成随机数的个数
* @return 返回生成 [0-n) 个不重复的随机数,用 list 来保存这些随机数
*/
public static List<Integer> getDiffNO(int n) {
// list 用来保存这些随机数
List<Integer> list = new ArrayList<Integer>();
Random rand = new Random();
boolean[] bool = new boolean[n];
int num = 0;
for (int i = 0; i < n; i++) {
do {
// 如果产生的数相同则继续循环
num = rand.nextInt(n);
} while (bool[num]);
bool[num] = true;
list.add(num);
}
return list;
}
}

运行结果

[0, 9, 2, 1, 4, 5, 3, 6, 7, 8]
[8, 5, 3, 4, 1, 2, 6, 7, 9, 0]
[4, 6, 1, 5, 3, 0, 8, 7, 9, 2]
[6, 7, 9, 8, 0, 1, 2, 5, 3, 4]
[5, 1, 4, 9, 0, 7, 8, 6, 3, 2]
[2, 8, 3, 1, 5, 4, 7, 0, 9, 6]
[7, 8, 4, 5, 1, 9, 3, 0, 6, 2]
[3, 1, 8, 2, 6, 0, 9, 4, 5, 7]
[2, 1, 0, 7, 4, 5, 8, 3, 9, 6]
[0, 4, 3, 1, 7, 6, 8, 5, 9, 2] Process finished with exit code 0

参考资料

生成 n 个不同的随机数且随机数区间为 [0,n)的更多相关文章

  1. 动态生成16位不重复随机数、随机创建2位ID

    /** 1. * 动态生成16位不重复随机数 * * @return */ public synchronized static String generate16() { StringBuffer ...

  2. shell 生成指定范围随机数与随机字符串 .

    shell 生成指定范围随机数与随机字符串         分类:             shell              2014-04-22 22:17     20902人阅读     评 ...

  3. random and password 在Linux下生成crypt加密密码的方法,shell 生成指定范围随机数与随机字符串

    openssl rand -hex n (n is number of characters) LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head ...

  4. python生成随机数、随机字符串

    python生成随机数.随机字符串 import randomimport string # 随机整数:print random.randint(1,50) # 随机选取0到100间的偶数:print ...

  5. JS生成指定范围内的随机数(支持随机小数)

    直接需要函数的话,直接到文章的最后面找. ============================================================= 转载:https://www.cn ...

  6. js生成1-100不重复的随机数及生成10个1-100不重复的随机数

    //生成1-100不重复的随机数 var count=100; var a=new Array(); for(var i=0;i<100;i++){ a[i]=i+1; } a.sort(fun ...

  7. JS生成限定整数区间范围内的随机整数

    对于整数区间获取随机整数: m,n均为整数,且n>m. 获取[m,n)区间内的随机整数: 1 var aNumber = (n - m) * Math.random() + m; 2 var r ...

  8. 关于 Math.random()生成指定范围内的随机数的公式推导

    关于 Math.random()生成指定范围内的随机数的公式推导 在 java 中,用于生成随机数的 Math 方法 random()只能生成 0-1 之间的随机数,而对于生成指定区间,例如 a-b ...

  9. js中Math.random()生成指定范围数值的随机数

    http://www.111cn.net/wy/js-ajax/57062.htm Math.random() 这个方法相信大家都知道,是用来生成随机数的.不过一般的参考手册时却没有说明如何用这个方法 ...

随机推荐

  1. 微信小程序的分享功能-css文字超过两行隐藏

    .info{ width:100px; word-break:break-all; display:-webkit-box; -webkit-box-orient:vertical; -webkit- ...

  2. SpatialHadoop的编译与运行

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/spatialhadoop_compile_and_run Spa ...

  3. sqlserver数据库查询语句

    --数据库所有表select * from sysobjects where type='u'; --指定表的所有列select name from syscolumns where id=(sele ...

  4. Tkinter 之MessageBox弹出框

    一.参数说明 语法 作用 截图 tk.messagebox.showwarning(title='提示', message='你确定要删除吗?') 警告信息弹窗   tk.messagebox.sho ...

  5. ICEM-缺角正方体(拓块)

    ​原视频下载地址:https://yunpan.cn/cqFIqQWrNqzNc  访问密码 6fb4

  6. C#读写西门子PLC数据

    C#读写西门子PLC数据,包含S7协议和Fetch/Write协议,s7支持200smart,300PLC,1200PLC,1500PLC 本文将使用一个gitHub开源的组件技术来读写西门子plc数 ...

  7. 教你阅读 Cpython 的源码(一)

    目录 第一部分-介绍 Cpython 源代码中有什么? 如何编译 Cpython 代码 编译器能做什么? 为什么 Cpython 是用 C 语言而是 Python 编写的? Python 语言的规范 ...

  8. 阿里云服务器发送邮件:Connection could not be established with host smtp.qq.com [Connection timed out #110]

    阿里云服务器发送邮件:Connection could not be established with host smtp.qq.com [Connection timed out #110] 一.总 ...

  9. Qtcreator远程调试出现“The selected build of GDB does not support Python scripting.It cannot be used .."

      版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/aristolto/article/details/77370853 之前使用的是Qt4.7后来换 ...

  10. php - ftp 上传文件到远程服务器

    ccentos7服务器 ======================== 一.安装vsftpd及ftp命令 yum install vsftpd -y yum install ftp -y 二.vsf ...