//一个利用random生成22选5×××的代码示例
public class Lottery
{
public static void main(String[] args)
{
int total = 22;
int k = 5;
int[] numbers = new int[total];
for(int i=0;i<numbers.length;i++)
{
numbers[i] = i+1;
}
int[] result = new int[k];
int counter = 0;
do
{
int r = (int)(Math.random()*22);
if(numbers[r]==-1)
continue;
result[counter]=numbers[r];
numbers[r]=-1;
counter++;
}
while(counter<5);
for(int i : result)
{
System.out.print(i+" ");
}
}
}
转载于:https://blog.51cto.com/brightmoon/1320498