简体   繁体   English

如何在0-9范围内生成三个随机数而无需从Android中的arraylist重复

[英]How to generate three random numbers in range 0-9 without repeating from arraylist in android

I store my numbers like that : 我这样存储我的号码:

ArrayList<Integer> nr=new ArrayList<Integer>();

    nr.add(0);
    nr.add(1);
    nr.add(2);
    nr.add(3);
    nr.add(4);
    nr.add(5);
    nr.add(6);
    nr.add(7);
    nr.add(8);
    nr.add(9);

    Random r1 = new Random();
    Random r2 = new Random();
    Random r3 = new Random();

   int rnd1 = r1.nextInt(nr.size());
    nr.remove(rnd1);
   int rnd2 = r2.nextInt(nr.size());
    nr.remove(rnd2);
   int rnd3 = r2.nextInt(nr.size());
    nr.remove(rnd3);

I try to remove the generated numbers but it keeps repeating after some tries 我尝试删除生成的数字,但经过一些尝试后,它会不断重复

The idea here is that you are generating a random number in the range 0..9. 这里的想法是,您正在生成范围为0..9的随机数。 Then you remove one element and you generate another random number in the range 0..8. 然后删除一个元素,并生成另一个范围为0..8的随机数。 You finish with generating a third number in the range 0..7. 您将生成第三个数字,范围是0..7。

The correct way would be to use the numbers that you generated as indexes for the elements. 正确的方法是使用您生成的数字作为元素的索引。 You take an element with this index: this is your "random" number. 您可以使用带有此索引的元素:这是您的“随机”数字。 Then you remove this element so that you couldn't take it again (in my case, I obtain and remove the element using one operation: "remove"). 然后删除该元素,以免再次使用它(在我的情况下,我使用一个操作“ remove”来获取和删除该元素)。

ArrayList<Integer> nr = new ArrayList<Integer>();

nr.add(0);
nr.add(1);
nr.add(2);
nr.add(3);
nr.add(4);
nr.add(5);
nr.add(6);
nr.add(7);
nr.add(8);
nr.add(9);

Random r = new Random();

int rndPos1 = r.nextInt(nr.size());
int rnd1 = nr.remove(rndPos1);

int rndPos2 = r.nextInt(nr.size());
int rnd2 = nr.remove(rndPos2);

int rndPos3 = r.nextInt(nr.size());
int rnd3 = nr.remove(rndPos3);

System.out.println(rnd1 + ", " + rnd2 + ", " + rnd3);

You could shuffle your ArrayList and choose the first 3 items from it like so: 您可以shuffle ArrayList并从中选择前3个项目,如下所示:

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;

class Main {
  public static void main(String[] args) {
    ArrayList<Integer> nr =new ArrayList<Integer>();
    nr.add(0);
    nr.add(1);
    nr.add(2);
    nr.add(3);
    nr.add(4);
    nr.add(5);
    nr.add(6);
    nr.add(7);
    nr.add(8);
    nr.add(9);
    System.out.println("The ArrayList looks like: " + Arrays.toString(nr.toArray()));
    System.out.println("Three unique random numbers from it are:");
    //shuffle the list
    Collections.shuffle(nr);
    for(int i=0; i<3; i++) {
      System.out.println(nr.get(i));
      nr.remove(i);
    }
    System.out.println("The ArrayList now looks like: " + Arrays.toString(nr.toArray()));
    Collections.sort(nr);
    System.out.println("Sorted: " + Arrays.toString(nr.toArray()));
  }
}

Example Usage: 用法示例:

The ArrayList looks like: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Three unique random numbers from it are:
7
8
5
The ArrayList now looks like: [6, 2, 3, 9, 4, 0, 1]
Sorted: [0, 1, 2, 3, 4, 6, 9]

Try it here! 在这里尝试

There are multiple things wrong with your code: 您的代码有很多错误:
1. You don't need to create new random everytime. 1.您不需要每次都创建新的随机数。
2. Because you get all the option before hand you might run in to Array out of bounds exception. 2.因为您已获得所有选项,所以可能会遇到Array out of bounds异常。

In a loop get random number and check till you have 3 unique numbers. 循环获得随机数并检查直到您拥有3个唯一数。
Your code will look like this : 您的代码将如下所示:

ArrayList<Integer> nr=new ArrayList<Integer>();
nr.add(0);
nr.add(1);
nr.add(2);
nr.add(3);
nr.add(4);
nr.add(5);
nr.add(6);
nr.add(7);
nr.add(8);
nr.add(9);

Random r1 = new Random();
for(nr.size != 7) {
  nr.remove(r1.nextInt(nr.size()));
}

Another good way of doing this would be : 这样做的另一个好方法是:

Collections.shuffle(nr);
nr.remove(0);
nr.remove(1);
nr.remove(2);

Random generates random numbers by a seed. Random通过种子生成随机数。 If 2 Random objects have the same seed, the random numbers they generate will be the same. 如果2个Random对象具有相同的种子,则它们生成的随机数将相同。 By default, this seed is the current system time. 默认情况下,此种子是当前系统时间。

Here, you created three Random objects: 在这里,您创建了三个Random对象:

Random r1 = new Random();
Random r2 = new Random();
Random r3 = new Random();

Since code runs very fast, they are created in the same millisecond so their seeds are the same. 由于代码运行速度非常快,因此它们是在同一毫秒内创建的,因此它们的种子是相同的。 As a result, they generate same random numbers. 结果,它们生成相同的随机数。

The first step to fix your code is to create just one Random : 修复代码的第一步是仅创建一个Random

Random r = new Random();

The next step is to fix the way you select the random numbers from the array list. 下一步是修复从数组列表中选择随机数的方法。 You should get the item at the index of rnd1 , rnd2 , rnd3 您应该在rnd1rnd2rnd3的索引处获得项目

int rnd1 = r.nextInt(nr.size());
int result1 = nr.get(rnd1);
nr.remove(rnd1);
int rnd2 = r.nextInt(nr.size());
int result2 = nr.get(rnd2);
nr.remove(rnd2);
int rnd3 = r.nextInt(nr.size());
int result3 = nr.get(rnd3);
nr.remove(rnd3);

result1-3 are your non-repeating random numbers! result1-3是您的非重复随机数!

I have helped you cleaned up your code: 我已经帮助您清理了代码:

int index = r.nextInt(nr.size());
int result1 = nr.get(rnd1);
nr.remove(index);
index = r.nextInt(nr.size());
int result2 = nr.get(rnd2);
nr.remove(index);
index = r.nextInt(nr.size());
int result3 = nr.get(rnd3);
nr.remove(index);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM