package day14; import java.util.Arrays; import java.util.Random; public class Demo01 { public static void main(String[] args) { int[] balls = gen(); System.out.println(Arrays.toString(balls)); } public static int[] gen() { int[] pool = new int[34]; for (int i = 1; i < pool.length; i++) { pool[i - 1] = i; } boolean[] used = new boolean[pool.length]; int[] balls = new int[6]; int index = 0; Random random = new Random(); int i; do { i = random.nextInt(pool.length); if (used[i]) continue; balls[index++] = pool[i]; used[i] = true; } while (index != balls.length); Arrays.sort(balls); balls = Arrays.copyOf(balls, balls.length + 1); balls[balls.length - 1] = pool[random.nextInt(16)]; return balls; } }
本文出自 “浪漫的偷笑” 博客,请务必保留此出处http://lmdtx.blog.51cto.com/6942028/1699872
原文地址:http://lmdtx.blog.51cto.com/6942028/1699872