Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Random;
- public class RandomArray {
- private static Random rng = new Random();
- public static int[] generate(int size) {
- List<Integer> ints = ints(size);
- int[] result = new int[size];
- int idx = 0;
- while (ints.size() > 0) {
- result[idx++] = ints.remove(random(ints.size()));
- }
- return result;
- }
- private static int random(int n) {
- return rng.nextInt(n);
- }
- // Creates List of size ints from 0-size - 1
- private static List<Integer> ints(int size) {
- List<Integer> ints = new ArrayList<Integer>();
- for (int i = 0; i < size; i++) {
- ints.add(i);
- }
- return ints;
- }
- public static void main(String[] args) {
- System.out.println(Arrays.toString(generate(10)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement