62,621
社区成员
发帖
与我相关
我的任务
分享System.out.println(new DecimalFormat("####").format(Math.random()*10000));List<Integer> list = new LinkedList<Integer>();
for (int i=0; i<10; i++) {
list.add(i); //把数字0-9放入集合
}
//取第一个不为0的数字
//因为上面知道0在头位置,所以随机取1位置以后的一个数字
int index = (int)(Math.random()*(list.size()-1)) + 1; //随机位置
int data = list.remove(index); //取出随机位置的数字,并把数字从集合中删除,这样再取就不会重复
Collections.shuffle(list); 随机打乱list的元素
for (int i=0; i<3, i++) { //从集合中取3个数字
data = data*10 + list.get(i);
}
System.out.println(data);
java.util.concurrent
Class ThreadLocalRandom
java.lang.Object
java.util.Random
java.util.concurrent.ThreadLocalRandom
All Implemented Interfaces:
Serializable
public class ThreadLocalRandom
extends Random
A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools.
Usages of this class should typically be of the form: ThreadLocalRandom.current().nextX(...) (where X is Int, Long, etc). When all usages are of this form, it is never possible to accidently share a ThreadLocalRandom across multiple threads.
This class also provides additional commonly used bounded random generation methods.
Since:
1.7
See Also:
Serialized Form
Method Summary
Methods
Modifier and Type Method and Description
static ThreadLocalRandom current()
Returns the current thread's ThreadLocalRandom.
protected int next(int bits)
Generates the next pseudorandom number.
double nextDouble(double n)
Returns a pseudorandom, uniformly distributed double value between 0 (inclusive) and the specified value (exclusive).
double nextDouble(double least, double bound)
Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
int nextInt(int least, int bound)
Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
long nextLong(long n)
Returns a pseudorandom, uniformly distributed value between 0 (inclusive) and the specified value (exclusive).
long nextLong(long least, long bound)
Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
void setSeed(long seed)
Throws UnsupportedOperationException.
public String getDigit(){
10. Random r=new Random();
11. int n=0;
12. while(n<SIZE){
13. String temp="";
14. int i=r.nextInt(10);
15. temp+=(char)('0'+i);
16. if(!digit.contains(temp)){
17. digit+=(char)('0'+i);
18. n++;
19. }
20. }
21. return digit;
22. }
public class Test {
static int[] arr = new int[4];
public static void main(String args[]) {
int i = 0;
while (i < 4) {
if (i == 0)
arr[i] = rand();
else {
arr[i] = rand();
while(a(i)) {// 如果有相等的則继续给第重新生成,直到没有相等
arr[i] = rand();
}
}
i++;
}
for(int a:arr){
System.out.print(a);
}
}
// 判断是否有何前面的相等
public static boolean a(int m) {
boolean flag = false;
for (int i = m-1; i >= 0; i--) {
if (arr[i] == arr[m])
flag = true;
}
return flag;
}
public static int rand() {
return (int) ((Math.random() * 9) + 1);
}
}