java 随机数

gqy281615 2012-07-11 06:46:56
用Math.random()产生各个数位不同的随机数,例如4位数。

...全文
481 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
q6823221 2012-07-17
  • 打赏
  • 举报
回复
[Quote=引用 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[……
[/Quote]写的不错
Mourinho 2012-07-17
  • 打赏
  • 举报
回复
[Quote=引用 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……
[/Quote]

+1
ld_7558082 2012-07-16
  • 打赏
  • 举报
回复
写的不错啊
Cactus_hxk 2012-07-13
  • 打赏
  • 举报
回复
System.out.println(new DecimalFormat("####").format(Math.random()*10000));
lliiqiang 2012-07-13
  • 打赏
  • 举报
回复
随机产生一个然后判断是否满足条件,如果不满足再产生直到产生符合条件的为止
qq247890212 2012-07-13
  • 打赏
  • 举报
回复
擦 ,没注意14楼的。你用他的吧直接
qq247890212 2012-07-13
  • 打赏
  • 举报
回复
可以将产生的随即数放进一个容器里如ArrayList。然后将后面产生的随机数用集合的contains();函数来判断一下前面有没有用过。用过就丢弃这个随机数,没用过则就放到集合里。最后够位数了就顺序从集合里取出来合成一个整数。实现起来应该比上面那么长代码的简单些。
lzc19891010 2012-07-12
  • 打赏
  • 举报
回复
(int)Math.Ramdom()* 1000
qybao 2012-07-12
  • 打赏
  • 举报
回复
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);
student-ai 2012-07-12
  • 打赏
  • 举报
回复
先产生一位随机数,然后存入数组,后面在产生。依次来。
Seanake 2012-07-12
  • 打赏
  • 举报
回复


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.
cjh_tostring 2012-07-12
  • 打赏
  • 举报
回复
自己多想下,动动手每个人都能写出来。
Sad4This 2012-07-12
  • 打赏
  • 举报
回复
以前写过猜数字游戏的一个功能和这个类似

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个不同数字,但是他是字符串,希望对楼主有帮助
gqy281615 2012-07-12
  • 打赏
  • 举报
回复
代码写的真不错 谢谢了
showjim 2012-07-12
  • 打赏
  • 举报
回复
random(10*9*8*7),混合进制
cjh_tostring 2012-07-12
  • 打赏
  • 举报
回复
[Quote=引用 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 = (……
[/Quote]
有勋章的人想法就是不同菜鸟。
qiutian1990 2012-07-12
  • 打赏
  • 举报
回复
(int)(9000*Math.random()) + 1000
cjh_tostring 2012-07-11
  • 打赏
  • 举报
回复
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);
}
}
wwwcomcn123 2012-07-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
这都知道 是使产生的各个数位不同 如产生的随机数1224就不行 因为2出现了俩次。
[/Quote]

用Math貌似不能达到这个效果,但用Random这个类可以
ZZZ5512536 2012-07-11
  • 打赏
  • 举报
回复
这个和双色球问题差不多。 你百度下吧
加载更多回复(5)

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧