This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: kberg on Feb 16th, 2012  |  syntax: Java 5  |  size: 0.84 KB  |  views: 390  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }
clone this paste RAW Paste Data