1

I have an array called arr, with place for 15 elements. I need to place the numbers 1 through 15 in a random order into that array. Here is what I have tried:

int[] arr = new int[15];
int i,j,k,n;

for (i = 0; i<15; i++) {
    for (j=0; j<15; j++) {
        n = (int)(Math.random() * 14 + 1);
        if (rij[j] != n) {
            rij[i] = n;
            break;
        }
    }
}

Thanks! :)

3
  • 1
    And does it do what you want? Commented Oct 29, 2011 at 17:24
  • nope, otherwise I wouldn't post it :p Commented Oct 29, 2011 at 17:25
  • The question means, that you should clarify what the problem is: Compile-time-error, exception, error-message, wrong result (which input, which output, what would be expected output), no result, ... Commented Oct 31, 2011 at 1:21

5 Answers 5

11

Use an ArrayList and fill it up with numbers 1 to 15.

Shuffle the list.

Convert it to an array.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, you had the same idea :-)
7

This seems like homework (or an interview question?). If that's the case and you are required to use arrays rather than the built in methods with the Java Collection Objects, (or even if not, really), the answer is the Fisher-Yates Shuffle algorithm

The modern in-place shuffle is:

To shuffle an array a of n elements (indexes 0..n-1):
for i from n − 1 downto 1 do
   j ← random integer with 0 ≤ j ≤ i
   exchange a[j] and a[i]

(I'd have to check, but I suspect this is what Java uses under the hood for its shuffle() methods).

Edit because it's fun to implement algorithms:

In java, this would be:

public static void main(String[] args) {

    int[] a = new int[15];
    for (int i = 1; i <= 15; i++)
    {
        a[i-1] = i;
    }

    Random rg = new Random();
    int tmp;
    for (int i = 14; i > 0; i--)
    {
        int r = rg.nextInt(i+1);
        tmp = a[r];
        a[r] = a[i];
        a[i] = tmp;
    }

    for (int i = 0; i < 15; i++)
        System.out.print(a[i] + " ");

    System.out.println();


}

And ... this can be further optimized using the inside-out version of the algo since you're wanting to insert a known series of numbers in random order. The following is the best way to achieve what you stated as wanting to do as there are no extra copies being made such as when creating an ArrayList and having it copy back out to an array.

a = new int[15];
Random rg = new Random();
for (int i = 0; i < 15; i++)
{
    int r = rg.nextInt(i+1);
    a[i] = a[r];
    a[r] = i+1;
}

2 Comments

Either way - that's the algo you're looking for to understand how to do it :)
One last edit showing the optimal way to do what you originally stated you wanted to do ;)
6

Do it like this

// Create an ordered list
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i < 16; i++) {
    list.add(i);
}

// Shuffle it
Collections.shuffle(list);

// Get an Integer[] array
Integer[] array1 = list.toArray(new Integer[list.size()]);

// Get an int[] array
int[] array2 = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
    array2[i] = list.get(i);
}

Comments

0

This will leave the elements randomly shuffled in a Integer[], if that's fine with you:

List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 15; i++)
    list.add(i + 1);

Collections.shuffle(list);
Integer[] arr = list.toArray(new Integer[0]);

Comments

0

I will do something like this:

First create a temporary arraylist filled with numbers from start to end, then using random select a number, copy it into array and remove it from the temp arraylist, repeat until the arraylist is empty...

    ArrayList<Integer> arr = new ArrayList<Integer>();
    int[] arr2 = new int[15];
    int i,j,k,n;

    for (i=0;i<15;i++) arr.add(i+1);
    i=0;
    while(arr.size()>0){                    
         n = (int)(Math.random() * (14 + 1 - i));
         arr2[i]=arr.get(n);
         arr.remove(n);
         i++;
    }   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.