双色球随机数

双色球随机数_第1张图片

package com.job;

public class randomtest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = new int[7];
		int num2 = (int) ((Math.random() * 16) + 1);// 1-16
		a[a.length - 1] = num2;
		for (int i = 0; i < a.length - 1; i++) {
			int num = (int) ((Math.random() * 33) + 1);// 1-33
			// 不能直接放到数组中
			// 判断是否有重复,如果有重复重新获取随机数
			while (chongfu(num, a)==true) {
				num = (int) ((Math.random() * 33) + 1);
			}
			// 放到数组
			a[i] = num;
		}
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
	}

	public static Boolean chongfu(int num, int[] a) {
		Boolean flag = false;
		for (int i = 0; i < a.length - 1; i++) {
			if (num == a[i]) {
				flag = true;
				break;
			}
		}
		return flag;

	}
}

 

你可能感兴趣的:(java)