Java 创建唯一随机数

Java 创建唯一随机数,java,random,Java,Random,我创建了以下方法,以便创建唯一的随机数。(此唯一值属于树的节点): 在您的检查方法中,这看起来有点不可靠: if (node.data == num) b = false; else b = true 一旦找到匹配项(例如b=false),您肯定要返回吗?否则,下一次循环b可能会设置为true。为了简化一点,如果您想检查某个项目是否在集合中,您可以执行列表。包含(元素)您的检查功能错误。目前,它只返回最后一个元素是否匹配num。一旦找到匹配项,就要声明true(例如,使用返回true

我创建了以下方法,以便创建唯一的随机数。(此唯一值属于树的节点):


在您的检查方法中,这看起来有点不可靠:

if (node.data == num) 
  b = false;
else
  b = true

一旦找到匹配项(例如b=false),您肯定要返回吗?否则,下一次循环b可能会设置为true。为了简化一点,如果您想检查某个项目是否在集合中,您可以执行列表。包含(元素)

您的
检查功能错误。目前,它只返回最后一个元素是否匹配
num
。一旦找到匹配项,就要声明
true
(例如,使用
返回true;


事实上,你可以在没有
b
的情况下做任何事情。我确信您可以使用
list
contain
方法来代替。

问题在于,如果check函数发现重复的数字,您不会停止for循环。循环继续,b可以变回true

您应该做的是,例如:

  private static boolean check(ArrayList<Node> list, int num) {
    for (Node node : list) {
        if(node.data == num)
            return false;
    }
    return true;
}
private静态布尔检查(ArrayList列表,int num){
用于(节点:列表){
if(node.data==num)
返回false;
}
返回true;
}

您应该将
检查方法更改为类似以下内容:

  private static boolean check(ArrayList<Node> list, int num)
  {
    for (Node node : list)
        if (node.data == num)
            return false;

    return true;
  }
private静态布尔检查(ArrayList列表,int num)
{
用于(节点:列表)
if(node.data==num)
返回false;
返回true;
}
通过这种方式,您可以浏览列表,找到相等的元素后立即返回
false
。如果您能够在不返回的情况下完成循环,则不会找到重复项,您可以返回
true

您“忘记”使用您准备的
numList

此代码应该可以正常工作:

static Random rand = new Random();

public static ArrayList<Node> go(int n) {
    ArrayList<Node> list = new ArrayList<Node>();
    ArrayList<Integer> numList = new ArrayList<Integer>();

    for (int i = 1; i <= 5; i++) {
        int number = rand.nextInt(10) + 1;
        if (numList.contains(number)) {
            i--;
            continue;
        }
        numList.add(number);
        Node node = new Node();
        node.data = number;
        list.add(node);
    }
    int w = 0;
    for (Node d : list) {
        System.out.println(w + ": " + d.data);
        w++;
    }
    return list;

}
static Random rand=new Random();
公共静态数组列表go(int n){
ArrayList=新建ArrayList();
ArrayList numList=新的ArrayList();

对于(inti=1;iJón Trausti Arason有你的答案,但是

由于允许值(整数)的数量有限,并且不希望重复拾取同一个值,因此可能更容易只拾取一个允许值数组。然后,您可以从数组中拾取下一个值,而不必担心每次是否重复


在您的示例中,选择1到10之间的5个值,您可以从数组
{1,2,3,4,5,6,7,8,9,10}
开始,并通过一次随机排列将其重新排列为类似
{3,4,7,1,10,9,5,8,2,6}的其他值
。从结果数组中取出前五个值,不必担心重复。

来说明@eaj的观点

public static List<Node> go(int n) {
    List<Integer> numbers = new ArrayList<Integer>();
    for (int i = 1; i <= 10; i++) numbers.add(i);
    Collections.shuffle(numbers);
    List<Node> nodes = new ArrayList<Node>();
    for (Integer data : numbers.subList(0, 5))
        nodes.add(new Node(data)); // use a constructor for Node.
    for (int w = 0; w < nodes.size(); w++)
        System.out.println(w + ": " + nodes.get(w).data);
    return nodes;
}
公共静态列表go(int n){
列表编号=新的ArrayList();
对于(int i=1;i这是我的解决方案:

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


public class comboGenerator {

    public static void main(String[] args) {


    ArrayList<Integer> $combo = new ArrayList<Integer>();       // init. array list combo for randomization


        while ($combo.size() < 6) {  
            int rand = (int) (Math.random()*49+1);          // make new random number 1-49
            if (!$combo.contains(rand)){                    // check if we have that number in array list,{
            $combo.add(rand);                               // if there is no such number then add it to array list
            Collections.sort($combo);                       // sort the array list small >> large
            }
        }

    System.out.println("Random combination " + $combo);

}   
}
import java.util.ArrayList;
导入java.util.Collections;
公共类组合生成器{
公共静态void main(字符串[]args){
ArrayList$combo=new ArrayList();//用于随机化的init.array列表组合
而($combo.size()<6){
int rand=(int)(Math.random()*49+1);//生成新的随机数1-49
如果(!$combo.contains(rand)){//检查数组列表中是否有该数字{
$combo.add(rand);//如果没有这样的数字,则将其添加到数组列表中
Collections.sort($combo);//对数组列表进行排序小>>大
}
}
System.out.println(“随机组合”+$combo);
}   
}

而且你不能得到相同的数字!

你不使用的原因是什么?SecureRandom提供了不可预测的序列。有许多非安全性使用Random,其中可预测性不是问题,也有一些是需要复制相同的序列。实际上应该是相反的:在重复和false
e> 真的
在endOops,我的糟糕!编辑!谢谢。谢谢!:)我忘记了洗牌在Collections API中。
public static List<Node> go(int n) {
    List<Integer> numbers = new ArrayList<Integer>();
    for (int i = 1; i <= 10; i++) numbers.add(i);
    Collections.shuffle(numbers);
    List<Node> nodes = new ArrayList<Node>();
    for (Integer data : numbers.subList(0, 5))
        nodes.add(new Node(data)); // use a constructor for Node.
    for (int w = 0; w < nodes.size(); w++)
        System.out.println(w + ": " + nodes.get(w).data);
    return nodes;
}
import java.util.ArrayList;
import java.util.Collections;


public class comboGenerator {

    public static void main(String[] args) {


    ArrayList<Integer> $combo = new ArrayList<Integer>();       // init. array list combo for randomization


        while ($combo.size() < 6) {  
            int rand = (int) (Math.random()*49+1);          // make new random number 1-49
            if (!$combo.contains(rand)){                    // check if we have that number in array list,{
            $combo.add(rand);                               // if there is no such number then add it to array list
            Collections.sort($combo);                       // sort the array list small >> large
            }
        }

    System.out.println("Random combination " + $combo);

}   
}