about random.next int();

Aron Jhed Amiscosa
Ranch Hand
Posts: 118
hi there guys.. i am given a task about java.util.Random...

i need to come up with an array with unique int values...
like, 1 can only appear once, 2 must appear once, so on and so forth...
i mean, no same value within the array...

any idea on how to do this guys?

thanks in advance..

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
So, you need an array of random, but unique integers?

One way you could do this is by making a loop for all the elements of the array, and in each iteration generate a random number, check if it is already in the array, and if it is not, then put it in the array and go to the next iteration; otherwise, generate another random number (until you get one that's not already in the array). This approach has a major disadvantage: in principle it could run forever, or it could take a long time before the random number generator creates a number that's not already in the array. It will probably work OK if the size of the array is small compared to the range of random integers that you're choosing from.

Another way is to fill a list with numbers (just 1, 2, 3, ..., etc.) and then shuffle the list using Collections.shuffle();.

Evan Caballero
Ranch Hand
Posts: 59
Jesper is right. It will be ok if your target array is not so big.

With what size do you plan to initialize your array ?

Aron Jhed Amiscosa
Ranch Hand
Posts: 118
hi there Jesper
thanks for the help.

i used the one you suggested. the Collections.shuffle();
i am using it with LinkedList<Integer>.

thanks a lot for the help.

@evan: its just an array of 10 values.

Rob Spoor
Sheriff
Posts: 20669
65
Jesper Young wrote:Another way is to fill a list with numbers (just 1, 2, 3, ..., etc.) and then shuffle the list using Collections.shuffle();.

That's how I would solve it.

Evan Caballero
Ranch Hand
Posts: 59
To generate a random array between 1 and 10, you can do it like Jesper suggested first, with a loop like this.

Aron Jhed Amiscosa
Ranch Hand
Posts: 118
yeah. thanks for the helps...

i came up with this one. and i am satisfied with it.

you all been a great help to me ever since.

Rob Spoor
Sheriff
Posts: 20669
65
You could also skip the List<Integer> and simply reimplement Collections.shuffle for arrays:
That will save you the extra List.

By the way, using an ArrayList will probably be faster in this case, as you will just be swapping a lot of elements.

Aron Jhed Amiscosa
Ranch Hand
Posts: 118
thanks again Mr. Rob...

that really helps me a lot now.

i am nearly done with my project...

Rob Spoor
Sheriff
Posts: 20669
65
I would include a comment that this is the shuffling algorithm as used by Collections.sort. It is available from the src.zip file that comes with the JDK installatation, and it informs your professor that you know to look for methods in the API first. After all, you need to understand your own solution. If you don't get the shuffling then use your old code (but with ArrayList).

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
That's the Fisher - Yates shuffling algorithm