1 public class Randomer { 2 3 public static void main(String[] args) { 4 Random rand = new Random(); 5 System.out.println(rand.nextInt(3)+1); 6 7 int[] a = {1, 1, 1, 1, 4, 4, 4, 5, 5, 7, 7, 7, 9}; 8 int start = 0; 9 int position; 10 for (int i = 0; i < a.length; i++) { 11 if (a[i] != a[start]) { 12 position = getRandomBetweenAB(start, i); 13 System.out.println(position + " : " + a[position]); 14 start = i; 15 } 16 } 17 position = getRandomBetweenAB(start, a.length); 18 System.out.println(position + " : " + a[position]); 19 } 20 21 private static int getRandomBetweenAB(int a, int b) { 22 Random rand = new Random(); 23 return (rand.nextInt(b - a) + a); 24 } 25 }
时间: 2024-11-09 15:11:04