Java 用基本技术生成10个无重复的随机数
我的意图是使用最简单的java(数组和循环)生成无重复的随机数…但结果是10个重复数,我不知道为什么 这是我的密码:Java 用基本技术生成10个无重复的随机数,java,arrays,loops,Java,Arrays,Loops,我的意图是使用最简单的java(数组和循环)生成无重复的随机数…但结果是10个重复数,我不知道为什么 这是我的密码: int[] number = new int[10]; int count = 0; int num; while (count < number.length) { num = r.nextInt(21); boolean repeat = false; do { for (int i=0; i<number.length;
int[] number = new int[10];
int count = 0;
int num;
while (count < number.length) {
num = r.nextInt(21);
boolean repeat = false;
do {
for (int i=0; i<number.length; i++) {
if (num == number[i]) {
repeat = true;
} else if (num != number[i] && i == count) {
number[count] = num;
count++;
repeat = true;
}
}
} while (!repeat);
}
for (int j = 0; j < number.length; j++) {
System.out.print(number[j] + " ");
}
int[]编号=新的int[10];
整数计数=0;
int-num;
while(计数 对于(int i=0;i您可以使用集合
来代替吗?如果您还想跟踪插入顺序,可以使用LinkedHashSet
Random r = new Random();
Set<Integer> uniqueNumbers = new HashSet<>();
while (uniqueNumbers.size()<10){
uniqueNumbers.add(r.nextInt(21));
}
for (Integer i : uniqueNumbers){
System.out.print(i+" ");
}
Random r=new Random();
Set uniqueNumbers=新HashSet();
while(uniqueNumbers.size()您的代码将在以下条件下中断while循环:num==number[i]
这意味着,如果伪生成的数字等于该位置值(java中默认的int
为0),则代码将结束执行
在第二个条件中,表达式num!=number[i]
始终为真(否则,如果,代码将输入上一个),但在第一次运行时,当i==count
(或i=0
,和count=0
)repeat=true
会中断循环,并且不会发生任何其他情况,从而使输出呈现如下内容
0 0 0 0 0 0...
试试这个:
int[] number = new int[10];
java.util.Random r = new java.util.Random();
for(int i=0; i<number.length; i++){
boolean repeat=false;
do{
repeat=false;
int num = r.nextInt(21);
for(int j=0; j<number.length; j++){
if(number[j]==num){
repeat=true;
}
}
if(!repeat) number[i]=num;
}while(repeat);
}
for (int k = 0; k < number.length; k++) {
System.out.print(number[k] + " ");
}
System.out.println();
int[]编号=新的int[10];
java.util.Random r=new java.util.Random();
对于(int i=0;i,如果满足任一条件,则需要将从for
循环中断开
int[] number = new int[10];
int count=0;
int num;
Random r = new Random();
while(count<number.length){
num = r.nextInt(21);
boolean repeat=false;
do{
for(int i=0; i<number.length; i++){
if(num==number[i]){
repeat=true;
break;
}
else if(i==count){
number[count]=num;
count++;
repeat=true;
break;
}
}
}while(!repeat);
}
for(int j=0;j<number.length;j++){
System.out.print(number[j]+" ");
}
int[]编号=新的int[10];
整数计数=0;
int-num;
随机r=新随机();
while(count我相信这个问题更容易解决。你可以使用一个列表来检查数字是否已经生成(唯一性)。下面是一个工作代码块
int count=0;
int num;
Random r = new Random();
List<Integer> numbers = new ArrayList<Integer>();
while (count<10) {
num = r.nextInt(21);
if(!numbers.contains(num) ) {
numbers.add(num);
count++;
}
}
for(int j=0;j<10;j++){
System.out.print(numbers.get(j)+" ");
}
}
int count=0;
int-num;
随机r=新随机();
列表编号=新的ArrayList();
(count让我们从最简单的方法开始,将10个可能重复的随机数字放入一个数组:
public class NonUniqueRandoms
{
public static void main(String[] args)
{
int[] number = new int[10];
int count = 0;
while (count < number.length) {
// Use ThreadLocalRandom so this is a contained compilable unit
number[count++] = ThreadLocalRandom.current().nextInt(21);
}
for (int j = 0; j < number.length; j++) {
System.out.println(number[j]);
}
}
}
公共类非查询和OMS
{
公共静态void main(字符串[]args)
{
int[]编号=新int[10];
整数计数=0;
while(计数
因此,在大部分情况下,您只需选择一个数字并检查数组:
public class UniqueRandoms
{
public static void main(String[] args)
{
int[] number = new int[10];
int count = 0;
while (count < number.length) {
// Use ThreadLocalRandom so this is a contained compilable unit
int candidate = ThreadLocalRandom.current().nextInt(21);
// Is candidate in our array already?
boolean exists = false;
for (int i = 0; i < count; i++) {
if (number[i] == candidate) {
exists = true;
break;
}
}
// We didn't find it, so we're good to add it to the array
if (!exists) {
number[count++] = candidate;
}
}
for (int j = 0; j < number.length; j++) {
System.out.println(number[j]);
}
}
}
公共类UniqueRandoms
{
公共静态void main(字符串[]args)
{
int[]编号=新int[10];
整数计数=0;
while(计数
问题在于您的内部“for”循环。一旦程序找到唯一的整数,它会将该整数添加到数组中,然后增加计数。在下一次循环迭代中,将再次添加新整数,因为(num!=number[i]&&i==count),最终用相同的整数填充数组。首次添加唯一整数后,for循环需要退出
但是,如果我们更深入地观察结构,我们会发现内部for循环完全没有必要
请参阅下面的代码
import java.util.*;
public class RandomDemo {
public static void main( String args[] ){
// create random object
Random r = new Random();
int[] number = new int[10];
int count = 0;
int num;
while (count < number.length) {
num = r.nextInt(21);
boolean repeat = false;
int i=0;
do {
if (num == number[i]) {
repeat = true;
} else if (num != number[i] && i == count) {
number[count] = num;
count++;
repeat = true;
}
i++;
} while (!repeat && i < number.length);
}
for (int j = 0; j < number.length; j++) {
System.out.print(number[j] + " ");
}
}
}
import java.util.*;
公开课随机演示{
公共静态void main(字符串参数[]){
//创建随机对象
随机r=新随机();
int[]编号=新int[10];
整数计数=0;
int-num;
while(计数
这将是我的方法
import java.util.Random;
public class uniquerandom {
public static void main(String[] args) {
Random rnd = new Random();
int qask[]=new int[10];
int it,i,t=0,in,flag;
for(it=0;;it++)
{
i=rnd.nextInt(11);
flag=0;
for(in=0;in<qask.length;in++)
{
if(i==qask[in])
{
flag=1;
break;
}
}
if(flag!=1)
{
qask[t++]=i;
}
if(t==10)
break;
}
for(it=0;it<qask.length;it++)
System.out.println(qask[it]);
}}
import java.util.Random;
公共类uniquerandom{
公共静态void main(字符串[]args){
随机rnd=新随机();
int qask[]=新int[10];
int it,i,t=0,in,flag;
for(it=0;it++)
{
i=下一个月的需求量(11);
flag=0;
for(in=0;in公共字符串pickStringElement(ArrayList列表,int…多少){
int计数器=多少。长度>0?多少[0]:1;
字符串returnString=“”;
ArrayList previousVal=新ArrayList()
对于(inti=1;i创建简单方法,并在需要时调用它-
private List<Integer> q_list = new ArrayList<>(); //declare list integer type
private void checkList(int size)
{
position = getRandom(list.size()); //generating random value less than size
if(q_list.contains(position)) { // check if list contains position
checkList(size); /// if it contains call checkList method again
}
else
{
q_list.add(position); // else add the position in the list
playAnimation(tv_questions, 0, list.get(position).getQuestion()); // task you want to perform after getting value
}
}
这会产生什么输出?“它不工作”不是问题的描述。你期望它做什么,它做什么?我猜你应该在if
块或else
块内将repeat
设置为false
,但我没有仔细阅读代码。如何使用真正的随机函数?以及gener
private List<Integer> q_list = new ArrayList<>(); //declare list integer type
private void checkList(int size)
{
position = getRandom(list.size()); //generating random value less than size
if(q_list.contains(position)) { // check if list contains position
checkList(size); /// if it contains call checkList method again
}
else
{
q_list.add(position); // else add the position in the list
playAnimation(tv_questions, 0, list.get(position).getQuestion()); // task you want to perform after getting value
}
}
public static int getRandom(int max){
return (int) (Math.random()*max);
}