Set<Integer> set = new HashSet<Integer>();
Random random = new Random();
while(set.size() < 10){
set.add(random.nextInt(999999));
}
为啥会生成6位以下的?
9 个解决方案
#1
如何限制随机数为 6-8位?
#2
set.add(100000 + random.nextInt(900000));
#3
最土的方法
Set<Integer> set = new HashSet<Integer>();
Random random = new Random();
while(set.size() < 10){
float f = random.nextFloat();
Integer i = (int) (f*100000000);
set.add(i);
}
#4
生成8位内的随机数rand
while(true){
if rand meets your need
break
}
while(true){
if rand meets your need
break
}
#5
朋友你好,Java提供的随机类是个非常强大的。你的代码random.nextInt(999999),是告诉编译器,
产生0~999999(不包括999999)的随机数,更改代码的话,二楼的代码便可以满足你需求。加油学习!
#6
让随机的去产生6位,7位,8位的随机数
Random random = new Random();
double ran = Math.random();
if (ran <= 0.33) { // 6 位
System.out.println(100000 + random.nextInt(900000));
} else if ((ran > 0.33) && (ran < 0.67)) { // 7 位
System.out.println(1000000 + random.nextInt(9000000));
} else { // 8 位
System.out.println(10000000 + random.nextInt(90000000));
}
#7
import java.util.Random;
/**
* 生成7-8位之间的随机数
*
*/
public class Test07 {
public static void main(String[] args) {
int max=10000000;
int min=1000000;
Random random = new Random();
int s = random.nextInt(max)%(max-min+1) + min;
System.out.println(s);
}
}
#8
(int)(100000+Math.random()*9900000)
#9
6~8位整数,也就是100000~99999999之间的随机数,等价于100000+(0~99899999)。
Random rand = new Random();
int n = 100000+rand.nextInt(99900000);
#1
如何限制随机数为 6-8位?
#2
set.add(100000 + random.nextInt(900000));
#3
最土的方法
Set<Integer> set = new HashSet<Integer>();
Random random = new Random();
while(set.size() < 10){
float f = random.nextFloat();
Integer i = (int) (f*100000000);
set.add(i);
}
#4
生成8位内的随机数rand
while(true){
if rand meets your need
break
}
while(true){
if rand meets your need
break
}
#5
朋友你好,Java提供的随机类是个非常强大的。你的代码random.nextInt(999999),是告诉编译器,
产生0~999999(不包括999999)的随机数,更改代码的话,二楼的代码便可以满足你需求。加油学习!
#6
让随机的去产生6位,7位,8位的随机数
Random random = new Random();
double ran = Math.random();
if (ran <= 0.33) { // 6 位
System.out.println(100000 + random.nextInt(900000));
} else if ((ran > 0.33) && (ran < 0.67)) { // 7 位
System.out.println(1000000 + random.nextInt(9000000));
} else { // 8 位
System.out.println(10000000 + random.nextInt(90000000));
}
#7
import java.util.Random;
/**
* 生成7-8位之间的随机数
*
*/
public class Test07 {
public static void main(String[] args) {
int max=10000000;
int min=1000000;
Random random = new Random();
int s = random.nextInt(max)%(max-min+1) + min;
System.out.println(s);
}
}
#8
(int)(100000+Math.random()*9900000)
#9
6~8位整数,也就是100000~99999999之间的随机数,等价于100000+(0~99899999)。
Random rand = new Random();
int n = 100000+rand.nextInt(99900000);