0

I have looked at Randomize or shuffle an array Randomize or shuffle an array

I am not sure if this is the best approach to make.

I want to randomize the indices of an array with 3 items.

12 4 5

int numbers[] = new int[3];

I tried using the Maths.Random

int randomoption2 = opmin + (int)(Math.random() * ((opmax - opmin) + 1));

but I then have an issue with repetition of the indices values. What is the best approach to randomize the indices so there is no repetition .

eg

a[1] = 2;

I don't want two elements in the array coming back with an indices of one

http://www.exampledepot.com/egs/java.util/coll_Shuffle.html

public class randomorder {

        public static void main(String [] args)
        {
            randomorder();  
            System.out.println(randomorder());
        }

        public static ArrayList randomorder(){
            ArrayList nums = new ArrayList();
            nums.add(1);
            nums.add(2);
            nums.add(3);

            Collections.shuffle(nums);
            return nums;
        }
    }

I now need to store each of the numbers in variables so they can be outputted

System.out.println(options[0]);

Community
  • 1
  • 1
alex
  • 398
  • 1
  • 6
  • 24

2 Answers2

6

Use Collections.shuffle:

Integer[] numbers = { 1, 2, 3, 4, 5 };
Collections.shuffle(Arrays.asList(numbers));

See it working online: ideone

It uses the Fisher-Yates shuffle internally. This is an efficient shuffling algorithm that won't give you duplicates.

Related

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Doesn't work with `int[]`, just tested. The array of primitives doesn't get shuffeld. Works fine with `Integer[]`... (To my own surprise) – Andreas Dolk Aug 08 '12 at 20:09
  • And now with an array of `int[]`, please :P - Casting from `int[]` to `Integer[]` obviously doesn't work ;) – Andreas Dolk Aug 08 '12 at 20:15
  • @Andreas_D: You are right. You can't have a `List`. Instead you get a `List`! The solution is to use `Integer[]` instead. Related: [Arrays.asList() not working as it should?](http://stackoverflow.com/questions/1467913/arrays-aslist-not-working-as-it-should). – Mark Byers Aug 08 '12 at 20:16
  • @alex: What do you mean by "then need to print out the values so each System.out.println contains a value". Is this for homework? – Mark Byers Aug 08 '12 at 20:40
  • its a piece of software I am writing. I have everything finished button the random number generator function. It is written in android but I wanted to separate the android framework from the Java code. I need the numbers that are shuffled to go into three radio buttons – alex Aug 08 '12 at 20:44
  • um just imagine their 3 System.out.pritnln() statements. I know where to go from there. – alex Aug 08 '12 at 20:47
  • I just want three System.out.println to output the values after they have been shuffled. – alex Aug 08 '12 at 20:49
0

Just keep three booleans through an array of booleans. Once you hit 0, 1, or 2 index set them to true.

Choose a random position and do while(boolean[number chosen] == true) redo your random choice.

programmer33
  • 642
  • 8
  • 19