• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about random.next int();

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks again Mr. Rob...

that really helps me a lot now.

i am nearly done with my project...

 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the Fisher - Yates shuffling algorithm
 
reply
    Bookmark Topic Watch Topic
  • New Topic