gpt4 book ai didi

java - 随机数生成器 : mainly how to stop repetition of numbers. Java

转载 作者:行者123 更新时间:2023-12-02 11:01:32 25 4
gpt4 key购买 nike

所以我正在开发一个 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;
}
}
}

这是我的程序,如果在没有输入 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.

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

谢谢大家的帮助!!!!

最佳答案

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

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

关于java - 随机数生成器 : mainly how to stop repetition of numbers. Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6709913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com