forked from bage2014/study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomUtils.java
More file actions
86 lines (69 loc) · 1.72 KB
/
Copy pathRandomUtils.java
File metadata and controls
86 lines (69 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.bage.study.java.random;
import java.util.Random;
public class RandomUtils {
public static void main(String[] args) {
int number = 30;
int min = 0;
int max = 30;
int randoms[] = getRandoms(number,min,max);
for (int i = 0; i < randoms.length; i++) {
if(i % 10 == 0) {
System.out.println("");
}
System.out.print(randoms[i] + "\t");
}
System.out.println("\n----------------------------------");
randoms = getRandoms(5,new int[] {1,2,3,4,5,6,7,8,9});
for (int i = 0; i < randoms.length; i++) {
if(i % 10 == 0) {
System.out.println();
}
System.out.print(randoms[i] + "\t");
}
}
/**
*
* @param number 随机数个数
* @param min 最小值[包含]
* @param max 最大值[包含]
* @return
*/
private static int[] getRandoms(int number, int min, int max) {
int n = max - min + 1;
int a[] = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = min + i ;
}
Random random = new Random();
int res[] = new int[number];
for (int i = 0; i < number; i++) {
int tempIndex = random.nextInt(n);
int temp = a[a.length - 1 - i];
a[a.length - 1 - i] = a[tempIndex];
a[tempIndex] = temp;
res[i] = a[a.length - 1 - i];
n -- ;
}
return res;
}
/**
*
* @param number 个数
* @param a 在a【】里面随机选择 number个
* @return
*/
private static int[] getRandoms(int number,int[] a) {
int n = a.length;
Random random = new Random();
int res[] = new int[number];
for (int i = 0; i < number; i++) {
int tempIndex = random.nextInt(n);
int temp = a[a.length - 1 - i];
a[a.length - 1 - i] = a[tempIndex];
a[tempIndex] = temp;
res[i] = a[a.length - 1 - i];
n -- ;
}
return res;
}
}