Random random = new Random();
(() % 100);
但是在运行的时候,发现上面的方法有时会产生负数,通过查看Random类的源代码才发现,不带参数的nextInt会产生所有有效的整数,所以当然会有负数产生了。
正确的解法应该是:
应用:Random random1 = new Random(); ((100)); //100是不包含在内的,只产生0~99之间的数。
生成乱序不重复数组
package ; import ; public class Arith { //对给定数目的自0开始步长为1的数字序列进行不重复乱序 public static int[] getSequence(int maxnum) { int[] sequence = new int[maxnum]; for(int i = 0; i < maxnum; i++){ sequence[i] = i; } // ((sequence)); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Random random = new Random(); for(int i = 0; i < maxnum; i++){ int p = (maxnum); int tmp = sequence[i]; sequence[i] = sequence[p]; sequence[p] = tmp; } random = null; return sequence; } //对给定数目的自minnum开始步长为1到maxnum的数字序列进行不重复乱序 public static int[] getSequence(int minnum,int maxnum) { int num = maxnum - minnum + 1; int[] sequence = new int[num]; for(int i = 0; i < num; i++){ sequence[i] = i + minnum; } // ((sequence)); Random random = new Random(); for(int i = 0; i < num; i++){ int p = (num); int tmp = sequence[i]; sequence[i] = sequence[p]; sequence[p] = tmp; } random = null; return sequence; } public static void main(String[] agrs){ Arith arith = new Arith(); int[] i = (10); for(int n=0;n<;n++){ (i[n]); } (); int[] j = (3,8); for(int n=0;n<;n++){ (j[n]); } } }