import java.util.Collections
import java.util.List
import java.util.Random
public class Main {
public static void main(String[] args) {
//红球 33 选6
List<Integer> redBall = new ArrayList<Integer>()
for(int i=0i<33i++){
redBall.add(i+1)
}
System.out.println("开奖红球:" + select(redBall, 6))
//篮球16选1
List<Integer> blueBall = new ArrayList<Integer>()
for(int i=0i<16i++){
blueBall.add(i+1)
}
System.out.println("开奖蓝球:" + select(blueBall, 1))
}
public static List<Integer> select(List<Integer> list,int count){
List<Integer> selectedList = new ArrayList<Integer>()
Random random = new Random()
for(int i=0i<counti++){
int index = random.nextInt(list.size())
Integer number = list.get(index)
selectedList.add(number)
list.remove(index)
}
Collections.sort(selectedList)
return selectedList
}
}
import java.util.Arraysimport java.util.Random
public class random {
public static void main(String[] args) {
int[] numArray = new int[30]
//生成一个存放30个数字的数组
for(int i=0i<numArray.lengthi++){
numArray[i]=i+1
}
Random random = new Random()
int randomNum = numArray.length-1
int[] array=new int[7]
for(int i=0i<array.lengthi++){
int index = random.nextInt(randomNum--)
array[i]= numArray[index]
int temp=numArray[index]
numArray[index]=numArray[randomNum]
numArray[randomNum]=temp
}
Arrays.sort(array)
for(int i=0i<array.lengthi++){
System.out.print(array[i]+" ")
}
}
说下大致思路,就是先弄一个1-30的数组,然后随机去数组里面取数,取完一次以后把本次取的数据往数组尾部放,并把尾部没选过的数字放到中间来,然后缩小检索数组的范围(这样就不会选到选过的数字了)
public class RandomDemo {public static void main(String[] args) {
LotteryTicket lt = new LotteryTicket()
int red[] = lt.getRed()
System.out.print("随机生成的红球为:")
for(int i=0i<6i++){
System.out.print(" "+red[i])
}
System.out.print("\t蓝球为:"+lt.getBlue())
}
}
class LotteryTicket{彩票生成类
private int[] red = new int[6]
public int[] getRed(){//返回红球
int i =0
for(i<6i++){
red[i] = (int)(Math.random()*33) +1
for(int j=0j<ij++){//保证了重复,我个从认为这个彩票好像不能重复吧
if(red[i]==red[j]){
red[i] = (int)(Math.random()*33) +1
j=0
}
}
}
return red
}
public int getBlue(){//返回蓝球
return (int)(Math.random()*16) +1
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)