双色球随机数

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;

	}
}

猜你喜欢

转载自blog.csdn.net/liujucai/article/details/81218814