Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Question - Picking Random Element from a Set

Options
  • 12-11-2007 12:14pm
    #1
    Closed Accounts Posts: 12,382 ✭✭✭✭


    Hello

    I hope someone can help me with this.

    If I have a set with 10 items in it, each item being a random number between 1 and 50, what would be the most efficent way of picking a random item from the set?

    I think with sets I have to declare which actual item I want to remove, rather than being able to say "take the fifth item from the set", right?

    Is there a way I can take the fifth item from the set without knowing what it's value is?

    Any help appreciated.

    Thanks


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    The thing with sets is that there's not necessarily any "fifth" item as best I understand it. A set defines a collection of objects but not their order or placement.

    The closest thing you can get is probably Set.toArray() which will give you an array containing the values of the set. You can then choose a random item from the array.


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    ** EDIT ^^^^ Or what Seamus said ^^^^^ (shouldn't make tea in the middle of a reply :D )

    The easiest way (probably not the quickest tho) that I can think is to use the toArray method to return an array of all the elements in the Set, then generate a random number between 0 and array.length -1 and select this element. This will give you the reference to the object you need to remove.

    One thing to be wary of is that there is no of knowing in which order the elements in the array will appear, as there is no concept of position within a set. Two successive same call to toArray could potentially produce two different orderings of the same elements.


  • Registered Users Posts: 85 ✭✭slavigo


    Not much of a java person myself but....

    I think anything implementing the Set Interface implements the Iterable interface.
    Get your "Iterator" and just call "next()" until you get to your randomly chosen element.

    I could be wrong here though. Any java heads out there able to varify?


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    slavigo wrote: »
    Not much of a java person myself but....

    I think anything implementing the Set Interface implements the Iterable interface.
    Get your "Iterator" and just call "next()" until you get to your randomly chosen element.

    I could be wrong here though. Any java heads out there able to varify?

    Yep you could generate a random number between 0 and set.size-1. Again the problem is that that without any concept of order (An element is either in a set or out of it, position doesn't apply) two iterators could potetially traverse the set in completely different ways.

    If the the order is actually important you could use a SortedSet.


  • Registered Users Posts: 85 ✭✭slavigo


    marco_polo wrote: »
    Yep you could generate a random number between 0 and set.size-1. Again the problem is that that without any concept of order (An element is either in a set or out of it, position doesn't apply) two iterators could potetially traverse the set in completely different ways.

    If the the order is actually important you could use a SortedSet.

    From reading the java API docs, I'm with the lads here (and generally, from just an awkwardly remembered definition of what a set is).

    I think you've got your methods now but also something else to think about.
    Maybe re implement with some sort of ordered collection as suggested above?
    Or go with what you have, It'll work for you but not exactly what it was designed for and the logic is a little off but as long as you can see why, at least thats something to take away. having said that, for correctness sake, maybe a quick look at SortedSet might be the best route.


  • Advertisement
  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Wow, thanks for the speedy and excellent replies.

    Lots to work with here, thanks.

    To answer some questions, the ordering of the items in the set is OK. I just want a random item from the set.

    I think the toArray method is probably the easiest (for me, at least!), so I will go with that.

    Thanks again for the help.

    PS I will take a look at SortedSet as suggested, and I agree that I have learnt something here :)


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    dublindude wrote: »
    Hello
    Is there a way I can take the fifth item from the set without knowing what it's value is?

    You should ask Heisenberg. If you know the item's position precisely, you can't possibly know it's value!

    http://en.wikipedia.org/wiki/Uncertainty_principle


Advertisement