• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Selecting five objects from an array

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,

i know i should rather be using an array list for what im doing here...i will do it with an array list after i get this right

here is my code:



how can i select five random objects from this array...is it done with a loop?
any ideas...

please dont give me code...just what i should be looking at...maybe a link to a page wih a clue

i understand random functions...i just cant work out how to gt it to select 5 from my array of 10

thanks
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you have an array of with 10 horses in it.
If you want to pick 5 out of the 10 horses.  
Consider what the result will be like.

1/  Do you just want 5 out of 10 horses all different? Meaning, if one horse is pick, it will be eliminated from the array.
If this is what you want, here is how I would go about this question and write code that best describe the pseudo code.
The steps may be differ, I am sure you'll figure it out.



2/  If you just want 5 random pick regardless of duplication, you can just randomly pick them with some form of repetition.
If this is what you want, like the previous pseudo code.  Except no need to remove the horse from the pool.



Does that give you some hints?
 
Frankie Law
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing.  If you choose option one.  The horse pool will shrink after you eliminate the horse from the pool.  If you don't want any horses remove from the pool but still not get duplicate.  Then you'll have to do additional comparison to make sure no duplicate is pick and pick until you get a different horse.

Hope this makes sense to you.
 
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It makes sense to me It is much easier to achieve that sort of thing with a List<Horse> or a Set<Horse> than a Horse[].
 
Sheriff
Posts: 28321
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It makes sense to me It is much easier to achieve that sort of thing with a List<Horse> or a Set<Horse> than a Horse[].



Yes... if you use a List<Horse> then you just call Collections.shuffle(thatList) and take the first 5 entries of the shuffled list.
 
Sheriff
Posts: 17677
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really had to use a Horse[], you don't need another array to hold the ones you picked out. You just have to move the picked horses to the end of the list, then narrow down the range from which you choose the next horse.

The pseudo code:

 
it's a teeny, tiny, wafer thin ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic