aspose file tools*
The moose likes Beginning Java and the fly likes about random.next int(); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about random.next int();" Watch "about random.next int();" New topic
Author

about random.next int();

Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
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

Joined: Aug 16, 2005
Posts: 14111
    
  16

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();.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Evan Caballero
Ranch Hand

Joined: Dec 10, 2009
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 ?
Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Evan Caballero
Ranch Hand

Joined: Dec 10, 2009
Posts: 59
To generate a random array between 1 and 10, you can do it like Jesper suggested first, with a loop like this.

Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.
Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
thanks again Mr. Rob...

that really helps me a lot now.

i am nearly done with my project...

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Aug 16, 2005
Posts: 14111
    
  16

That's the Fisher - Yates shuffling algorithm
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about random.next int();