随机数发生器:主要是如何停止重复数字.Java的

Chr*_*is 2 java random

因此,我正在开发一个OOP程序,该程序旨在通过使用随机数生成器创建50个唯一数字,确保不重复数字.我有随机部分,我正在使用一个额外的方法交换数字,但我不知道只交换数字,如果他们已经使用,我希望这是可以理解的.

import java.util.Random;
public class Project_1
{

    public static void main(String[] args)
    {
        Random rand = new Random();
        int a[][] = new int[11][6];
        for (int i = 1; i <= 10; i++) //row of values which fill in Student number
        {
            System.out.print("Student " + i + ":");
            System.out.print("\t");
            for (int j = 1; j <= 5; j++) //j is limited up to 5 columns
            {
                a[i][j] = 1 + rand.nextInt(50);
                CheckNumbers(a[i][j]); //input of the checkNumbers method
                System.out.print(a[i][j] + "\t"); // meaning that the numbers fill out according to table coordinates
            }
            System.out.println();
        }
    }

    public static void CheckNumbers(int[] x, int[] y)
    {
        int temp;
        for (int j = 0; j < 50; j++) //j must start at 1??? and we have 50 iterations, so countercontrolled to 50?
        {
            temp = x[i]; //this can be used to swap the numbers
            x[i] = y[i];
            y[i] = temp;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的程序,如果在没有输入CheckNumbers方法的情况下运行,我的回答是

 ----jGRASP exec: java Project_1

Student 1:  8   35  8   5   40  
Student 2:  46  3   44  40  8   
Student 3:  27  47  28  11  3   
Student 4:  30  25  43  8   34  
Student 5:  3   12  45  6   5   
Student 6:  19  37  33  14  14  
Student 7:  9   31  6   39  32  
Student 8:  16  6   23  28  31  
Student 9:  19  34  49  42  11  
Student 10: 26  3   17  16  15  

 ----jGRASP: operation complete.
Run Code Online (Sandbox Code Playgroud)

然而,你看到8重复(可能在其他数字之间)所以我放入CheckNumbers方法来阻止它但我有一个错误填充编译消息...所以我假设我的CheckNumbers中有很多错误,所以我需要对此也有重大帮助.

感谢大家的帮助!!!!

ant*_*oft 5

方便的方式来获得随机顺序的非重复数字列表:

使用1到50的整数创建一个ArrayList

使用Collections.Shuffle

(你也应该修复你的嵌套循环和分配,这样你的索引始终为0 - 但要了解你应该自己做一些)


Mar*_*aux 5

我不会纠正你的方法.但是,如果要创建随机而不重复,请创建自己的自定义类.

import java.util.BitSet;
import java.util.Random;

/**
 * Random number generator without repeat in the given range at construction time.
 * 
 * @author martijn
 */
public class NoRepeatRandom {

    private Random random;
    private BitSet used;
    private int max;

    /**
     * Creates new instance of NoRepeatRandom, with the range <code>[0-max[</code>.
     * @param max the maximum for the range
     * @param seed the seed for the underlying {@link java.util.Random}
     */
    public NoRepeatRandom(int max, long seed)
    {
        this.max = max;
        this.used = new BitSet(max);
        this.random = new Random(seed);
    }

    /**
     * Creates new instance of NoRepeatRandom, with the range <code>[0-max[</code>.<br />
     * <code>System.currentTimeMillis()</code> is used as seed for the underlying {@link java.util.Random}
     * @param max the maximum for the range
     */
    public NoRepeatRandom(int max)
    {
        this(max, System.currentTimeMillis());
    }

    /**
     * Gives the next random number
     * @return a new random number. When finished it returns -1.
     */
    public int next()
    {
        if (isFinished())
        {
            return -1;
        }
        while (true)
        {
            int r = random.nextInt(max);
            if (!used.get(r))
            {
                used.set(r);
                return r;
            }
        }
    }

    /**
     * Tells if the random generator has finished. Which means that all number in the range
     * [0-max[ are used.
     * @return true if all numbers are used, otherwise false.
     */
    public boolean isFinished()
    {
        return max == used.cardinality();
    }

    /**
     * Sets all the numbers in the range [0-max[ to unused. Which means that all the numbers
     * can be reused.
     */
    public void reset()
    {
        used.clear();
    }

    /**
     * 
     * @return the maximum.
     */
    public int getMax()
    {
        return max;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

NoRepeatRandom myRandom = new NoRepeatRandom(50);
for (int i = 0; i < myRandom.getMax(); ++i)
    System.out.println(myRandom.next());
Run Code Online (Sandbox Code Playgroud)

编辑:JavaDoc添加!!