Java乐透模拟
我正在编写一个程序,模拟1到45之间的6个数字的乐透抽奖,一个示例输出是3711274328。但我想做的是计算相邻数字出现的次数,例如1 4 5 29 26 41是肯定的答案,因为5在4之后 最好的方法是什么 我尝试过以下例子:Java乐透模拟,java,arrays,loops,Java,Arrays,Loops,我正在编写一个程序,模拟1到45之间的6个数字的乐透抽奖,一个示例输出是3711274328。但我想做的是计算相邻数字出现的次数,例如1 4 5 29 26 41是肯定的答案,因为5在4之后 最好的方法是什么 我尝试过以下例子: int adjacent=0; for(int i =0; i<6; i++) { int t = test[i]+1; test[i]=(int)(45*Math.random())+1; if
int adjacent=0;
for(int i =0; i<6; i++)
{
int t = test[i]+1;
test[i]=(int)(45*Math.random())+1;
if(test[i]==t)
adjacent++;
System.out.print(test[i]+" ");
}
这是行不通的
我做错了什么?我想你应该洗牌,然后再拿五个。CollectionsShuffle将帮助您,它使用默认的随机性源排列指定的列表。所有排列都以近似相等的可能性发生
List<Integer> list = ArrayList<Integer>();
list.add(1);
list.add(2);
Collections.shuffle(list);
Random rnd = new Random();
Integer[] result = new Integer[5];
result[0] = list.get(rnd.getNextInt(45));
result[1] = list.get(rnd.getNextInt(45));
result[2] = list.get(rnd.getNextInt(45));
result[3] = list.get(rnd.getNextInt(45));
result[4] = list.get(rnd.getNextInt(45));
现在您可以编写一个循环来查找相邻的数字
int adjacent = 0;
for(int i=1; i<result.length;i++){
int prev = result[i-1];
int now = result[i];
if(prev+1 == now)
adjacent++;
}
生成6个数字并将它们放入数组后。使用Arrays.sort。然后可以比较相邻的数组项
您还应该避免使用Random生成6个数字,因为它可以生成重复的数字。这可能会也可能不会准确地模拟您的彩票抽奖。Quoi的回答对此有一个很好的建议。我想你只是有一个操作顺序问题
int adjacent=0;
for(int i =0; i<6; i++)
{
//test[i] hasn't been set yet
int t = test[i]+1;
test[i]=(int)(45*Math.random())+1;
//this comparison doesn't make a whole lot of sense
if(test[i]==t)
adjacent++;
System.out.print(test[i]+" ");
}
将其更改为以下内容:
int adjacent=0;
for(int i =0; i<6; i++)
{
test[i]=(int)(45*Math.random())+1;
int t = -1;
//Make sure this comparison only happens after the second iteration
//to avoid index out of bounds
if ( i != 0 )
{
//Set t to the last number + 1 instead of trying to predict the future
t = test[i-1] + 1;
}
//Now this comparison makes a little more sense
//The first iteration will compare to -1 which will always be false
if(test[i]==t)
adjacent++;
System.out.print(test[i]+" ");
}
这可以进一步简化为:
int adjacent=0;
for(int i =0; i<6; i++)
{
test[i]=(int)(45*Math.random())+1;
if(i != 0 && test[i]==(test[i-1]+1))
adjacent++;
System.out.print(test[i]+" ");
}
您需要将unique的生成与下面的哈希集分开,以确保标识随机选择,对它们进行排序,然后确定邻接:
import java.util.HashSet;
import java.util.Arrays;
public class Lotto
{
public Lotto()
{
}
/**
* @param args
*/
public static void main(String[] args)
{
Lotto lotto = new Lotto();
lotto.randomizeSelections(5);
}
private void randomizeSelections(int numOfArrays)
{
for(int i = 0; i < numOfArrays; i++)
{
int[] selArry = new int[6];
//to insure that each random selection is unique
HashSet<Integer> idntySet = new HashSet<Integer>();
for(int j = 0; j < 6;)
{
int rndm = (int)(45 * Math.random()) + 1;
//add selection to the array only if it has not been randomized before
if(!idntySet.contains(rndm))
{
selArry[j] = rndm;
j++;
}
}
//sort the array for determing adjacency
Arrays.sort(selArry);
for(int j = 0; j < 6; j++)
{
int sel = selArry[j];
boolean isAdjcnt = (j > 0 && (sel == selArry[j - 1] + 1)) ? true : false;
System.out.println(i + "." + j + ".random = " + sel);
if(isAdjcnt) System.out.println("\tAdjacent");
}
}
}
}
输出值是1 4 29 5 26 41,你还会数数4和5吗?不,它们必须在一起,结果的顺序是否重要?你的乐透可以有重复吗?是的,数字必须按升序排列,比如说先抽4,然后是5,这是一个积极的例子,不,乐透不能有重复,但我知道如何消除这种情况。我不确定这是否回答了他的问题。但这比使用随机生成6个乐透号码要好。问题是检测相邻值,而不是如何首先生成随机号码。是的,我只是想展示正确的方法。不管怎么说,“-ve now”有什么原因吗?例如,如果先画8,再画7,这会给我一个肯定的结果,或者我误解了代码。@user1816464应该是相反的,我在缩短的版本中漏掉了一个加号,现在已经修复了。谢谢你现在可以了!!谢谢你的解释和帮助!
import java.util.HashSet;
import java.util.Arrays;
public class Lotto
{
public Lotto()
{
}
/**
* @param args
*/
public static void main(String[] args)
{
Lotto lotto = new Lotto();
lotto.randomizeSelections(5);
}
private void randomizeSelections(int numOfArrays)
{
for(int i = 0; i < numOfArrays; i++)
{
int[] selArry = new int[6];
//to insure that each random selection is unique
HashSet<Integer> idntySet = new HashSet<Integer>();
for(int j = 0; j < 6;)
{
int rndm = (int)(45 * Math.random()) + 1;
//add selection to the array only if it has not been randomized before
if(!idntySet.contains(rndm))
{
selArry[j] = rndm;
j++;
}
}
//sort the array for determing adjacency
Arrays.sort(selArry);
for(int j = 0; j < 6; j++)
{
int sel = selArry[j];
boolean isAdjcnt = (j > 0 && (sel == selArry[j - 1] + 1)) ? true : false;
System.out.println(i + "." + j + ".random = " + sel);
if(isAdjcnt) System.out.println("\tAdjacent");
}
}
}
}