随机数生成器:主要是如何停止数字的重复.爪哇 [英] Random Number Generator: mainly how to stop repetition of numbers. Java

查看:87
本文介绍了随机数生成器:主要是如何停止数字的重复.爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm working on an OOP program that is meant to create 50 unique numbers, by using a random number generator that ensures no repeating numbers. I have the random part down and I'm using an extra method which swaps the numbers but I don't know to only swap number if they are already used, I hope this is understandable.

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;
        }
    }
}

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

This is my program and if ran without the input of the CheckNumbers method my answer was

 ----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.

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

However as you see 8 is repeated (among may other numbers) so I put in the CheckNumbers method to stop it but I have an error filled compile message... so I am assuming that I have many errors in my CheckNumbers, so I need major help with that too.

谢谢大家的帮助!!!!

Thank you all for your help!!!!

推荐答案

我不会纠正你的方法.但如果你想创建一个不重复的随机,创建你自己的自定义类.

I'm not going to correct your methods. But if you want to create a random without repeating, create your own custom class.

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;
    }
}

用法:

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

添加了 JavaDoc!!

这篇关于随机数生成器:主要是如何停止数字的重复.爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆