您的位置:首页 > 其它

利用random函数随机产生不重复的数字

2018-03-08 14:40 387 查看
import java.util.Random;/**
*通过调用random函数,检测随机产生的数字,如果出现过,就标记flag为 true,依次作为鉴别标准
*/
public class random {public static void main(String[] args) {Random rand = new Random();//新建一个随机类boolean[]  bool = new boolean[20];int randInt = 0;//新建变量用于临时存储产生的随机数for(int i = 0; i < 6 ; i++) {do {//randInt  = rand.nextInt(20);//产生一个随机数randInt = (int) (Math.random()*15);}while(bool[randInt]);bool[randInt] = true;System.out.println(randInt);}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  随笔
相关文章推荐