我正在尝试根据用户提供的特定范围打印五个永不重复的随机数字。以下是我到目前为止的代码。我昨晚问了一个类似的问题并得到了一些很好的答案,但现在我只想使用数组和循环来完成这个。感谢。
package h1p2;
public class test{
public void method (int min, int max){
//Declare and initialize arrays and index variables
int rangeOne[];
int rangeMinMax[];
rangeOne = new int[5];
rangeMinMax = new int[max - min];
int z = min;
int i = 0;
int q = 0;
int rangeLength = rangeMinMax.length;
//need minimum/max differential of 50 or > to execute method
if (max - min < 50){
System.out.println("Please enter numbers with a differential of 50 or greater.");
}
//run if differential checks out
else{
//populate MinMax array with range specified by user
while (i < rangeLength){
rangeMinMax[i] = z;
z++;
i++;
}
}
//pick random number from MinMax array
int randomNumber = (rangeMinMax[(int) (rangeLength * Math.random())]);
int r = 0;
//populate rangeOne array with lotto numbers, forbidding duplicates
while (r < 5){
randomNumber = (rangeMinMax[(int) (rangeLength * Math.random())]);
rangeOne[r] = randomNumber;
randomNumber = (rangeMinMax[(int) (rangeLength * Math.random())]);
if (r == 1 && randomNumber == rangeOne[0]){
r--;
}
if (r == 2 && (randomNumber == rangeOne[0] || randomNumber == rangeOne[1])){
r--;
}
if (r == 3 && (randomNumber == rangeOne[0] || randomNumber == rangeOne[1] ||
randomNumber == rangeOne[2])){
r--;
}
if (r == 4 && (randomNumber == rangeOne[0] || randomNumber == rangeOne[1] ||
randomNumber == rangeOne[2] || randomNumber == rangeOne[3])){
r--;
}
else{
r++;}
}
//create string with results
String results = Integer.toString(rangeOne[0]) + " " + Integer.toString(rangeOne[1])
+ " " + Integer.toString(rangeOne[2]) + " " + Integer.toString(rangeOne[3]) +
" " + Integer.toString(rangeOne[4]);
//print results
System.out.println("MegaNumbers: " + results);
}
}
答案 0 :(得分:2)
如果您只需要少量数字,只需使用线性搜索来模拟一组。然后继续生成随机数,直到您生成足够的唯一数字就足够了。显然这不是最有效的,但很简单。
private static Random rand = new Random();
public static boolean contains(int [] data, int value)
{
for (int i = 0; i < data.length; i++)
{
if (data[i] == value)
{
return true;
}
}
return false;
}
public static int[] getRandom(int size, int lowerBound, int upperBound)
{
if (upperBound - lowerBound <= size)
{
throw new IllegalArgumentException("Range is too small!");
}
int totalRandoms = 0;
int[] randoms = new int[size];
while (totalRandoms != size)
{
int randNumber = rand.nextInt(upperBound - lowerBound) + lowerBound;
if (! contains(randoms, randNumber))
{
randoms[totalRandoms] = randNumber;
}
}
return randoms;
}
P.S。我已经把一些细节留给了你,我刚刚提供了几个非OO函数来让你对可能的算法有所了解。
答案 1 :(得分:1)
我写了randomList()来接受三个参数:
1)最小随机值
2)最大随机值
3)以及数组应具有的随机值的数量。
该方法首先创建一个名为choices的数组,其中包含从最小值到最大值的所有值。然后它启动一个循环,用来自选择的值填充新数组。每当循环从选择中选择一个随机值时,它会将该值移动到choices数组的后面并更新lastIndex,这样它就不再从选择的后面选择任何值。
更简单的方法是选择一个ArrayList并简单地从中删除已经获取的值。但是因为我们不能使用ArrayLists,所以这段代码应该可以正常工作。
public static int[] randomList(int min, int max, int number) {
// fills an array with all numbers from min to max
int[] choices = new int[max - min + 1];
int lastIndex = choices.length - 1;
for(int i = min; i <= max; i++) {
choices[i - min] = i;
}
// fills the new array with values from choices
Random r = new Random();
int[] randomList = new int[number];
for(int i = 0; i < number; i++) {
int index = r.nextInt(lastIndex + 1);
randomList[i] = choices[index];
int copy = choices[lastIndex];
choices[lastIndex] = randomList[i];
choices[index] = copy;
lastIndex--;
}
return randomList;
}