Advertisement
kberg

Untitled

Feb 16th, 2012
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. public class RandomArray {
  7.   private static Random rng = new Random();
  8.  
  9.   public static int[] generate(int size) {
  10.     List<Integer> ints = ints(size);
  11.     int[] result = new int[size];
  12.     int idx = 0;
  13.     while (ints.size() > 0) {
  14.       result[idx++] = ints.remove(random(ints.size()));
  15.     }
  16.     return result;
  17.   }
  18.  
  19.   private static int random(int n) {
  20.     return rng.nextInt(n);
  21.   }
  22.  
  23.   // Creates List of size ints from 0-size - 1
  24.   private static List<Integer> ints(int size) {
  25.     List<Integer> ints = new ArrayList<Integer>();
  26.     for (int i = 0; i < size; i++) {
  27.       ints.add(i);
  28.     }
  29.     return ints;
  30.   }
  31.  
  32.   public static void main(String[] args) {
  33.     System.out.println(Arrays.toString(generate(10)));
  34.   }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement