当前位置:编程学习 > JAVA >>

java 随机数

  用Math.random()产生各个数位不同的随机数,例如4位数。

--------------------编程问答-------------------- math.random()产生的是0~1之间的数,你后面乘以10000就ok了 --------------------编程问答-------------------- 1000+(int)(9000*Math.random()) 产生的是1000到9999的随机数 --------------------编程问答-------------------- int(Math.random()*10000) --------------------编程问答-------------------- 这都知道 是使产生的各个数位不同  如产生的随机数1224就不行  因为2出现了俩次。 --------------------编程问答--------------------
引用 4 楼  的回复:
这都知道 是使产生的各个数位不同 如产生的随机数1224就不行 因为2出现了俩次。

不能一下生成这样的,只能一位一位判断了,后一下与前一位不同,才组合成随机数 --------------------编程问答-------------------- 这个和双色球问题差不多。 你百度下吧
--------------------编程问答--------------------
引用 4 楼  的回复:
这都知道 是使产生的各个数位不同 如产生的随机数1224就不行 因为2出现了俩次。


用Math貌似不能达到这个效果,但用Random这个类可以 --------------------编程问答--------------------
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);
}
}
--------------------编程问答-------------------- 代码写的真不错  谢谢了 --------------------编程问答-------------------- 以前写过猜数字游戏的一个功能和这个类似

 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.    }  

这是生成4个不同数字,但是他是字符串,希望对楼主有帮助 --------------------编程问答-------------------- 自己多想下,动动手每个人都能写出来。 --------------------编程问答--------------------


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.
--------------------编程问答-------------------- 先产生一位随机数,然后存入数组,后面在产生。依次来。 --------------------编程问答-------------------- 4位数由没有讲究?比如第一个数字不能是0?

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);
--------------------编程问答-------------------- (int)Math.Ramdom()* 1000 --------------------编程问答-------------------- (int)(9000*Math.random()) + 1000 --------------------编程问答--------------------
引用 14 楼  的回复:
4位数由没有讲究?比如第一个数字不能是0?

Java code
List<Integer> list = new LinkedList<Integer>();
for (int i=0; i<10; i++) {
    list.add(i); //把数字0-9放入集合
}
//取第一个不为0的数字
//因为上面知道0在头位置,所以随机取1位置以后的一个数字
int index = (……

有勋章的人想法就是不同菜鸟。 --------------------编程问答-------------------- --------------------编程问答-------------------- random(10*9*8*7),混合进制 --------------------编程问答-------------------- 可以将产生的随即数放进一个容器里如ArrayList。然后将后面产生的随机数用集合的contains();函数来判断一下前面有没有用过。用过就丢弃这个随机数,没用过则就放到集合里。最后够位数了就顺序从集合里取出来合成一个整数。实现起来应该比上面那么长代码的简单些。 --------------------编程问答-------------------- 擦 ,没注意14楼的。你用他的吧直接 --------------------编程问答-------------------- 随机产生一个然后判断是否满足条件,如果不满足再产生直到产生符合条件的为止 --------------------编程问答--------------------
System.out.println(new DecimalFormat("####").format(Math.random()*10000));
--------------------编程问答-------------------- 写的不错啊 --------------------编程问答--------------------
引用 8 楼  的回复:
Java code
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[……
写的不错 --------------------编程问答--------------------
引用 14 楼  的回复:
4位数由没有讲究?比如第一个数字不能是0?


Java code
List<Integer> list = new LinkedList<Integer>();
for (int i=0; i<10; i++) {
    list.add(i); //把数字0-9放入集合
}
//取第一个不为0的数字
//因为上面知道0在头位置,所以随机取1位置以后的一个数字
int in……


+1 
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,