public class test { public static int[] Randoms(int number) { Random rand = new Random(); //创建一个新随机数生成器 int nu[] = new int[7]; //创建一个7位的数组,主要是保存结果,我需要7个不重复的随机值 boolean[] bool = new boolean[number + 1]; #+1是因为在下面随机数中我为了避开传入的数,不从0开始,所以+1,如果现在这里不加1,会提示越…
import java.text.SimpleDateFormat;import java.util.Date; public class Test2 { public static void main(String[] args){ SimpleDateFormat formatter2 = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss "); SimpleDateFormat formatter = new SimpleDateFormat…
我们知道 Random random = new Random() 中可能会获取到重复的随机数 那么假设要获取1到33之间的六个不重复随机数应该怎么做呢? 首先定义一个数字数组存储1到33 int[ ] redBall = new int[33[; for(int i = 0;i<redBall.length;i++){ redBall[i] = i+1; } int[ ] redNumber = new int[6]; //存储六个随机数的实际数组 int index = -1;…
在开发时要给某些表加上编号,而且编号是唯一的,自己用时间生成了下,觉得可能存在并发情况.所以在网上查了一下,就是随机生成.方法如下: //方法一(用当前时间精确到毫秒,截取任意几位) Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSS"); String formDate =sdf.format(date); System.out.println(formDate)…
java生成指定范围的随机数 package edu.sjtu.erplab.io; import java.util.Random; public class RandomTest { public static void main(String[] args) { int max=20; int min=10; Random random = new Random(); int s = random.nextInt(max)%(max-min+1) + min; System.out.pri…
代码来自:https://www.cnblogs.com/ningvsban/p/3590722.html,感觉实现的方式不错(做了一点小小修改) public static ArrayList getDiffNo(int count, int n){ // 生成count个[0-n) 不重复的随机数,左开右闭 // list 用来保存这些随机数 ArrayList list = new ArrayList(); Random rand = new Random(); boolean[] boo…
private static void testC(int sz) { long startTime = System.currentTimeMillis(); //开始测试时间 Random rd = new Random(); int[] rds = new int[sz];//随机数数组 List<Integer> lst = new ArrayList<Integer>();//存放有序数字集合 int index = 0;//随机索引 for (int i = 0; i…
Random rand = new Random(); boolean[] bool = new boolean[100]; int[] number = new int[100]; int randINT; for(int i = 0;i<100;i++){ do{ randINT = rand.nextInt(99); }while(bool[randINT]); number[randINT] = randINT; bool[randINT] = true; }…