943,748 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1350
  • Java RSS
Nov 12th, 2008
0

What's the most efficent way to randomize a Queue?

Expand Post »
I've seen a lot of different possible ways to do so, but I was wondering what people thought the most efficient way was.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adaykin is offline Offline
73 posts
since Feb 2007
Nov 12th, 2008
0

Re: What's the most efficent way to randomize a Queue?

What do you mean? A queue means its FIFO - first item put in the queue is the first item that comes out of the queue. So its not random.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 12th, 2008
0

Re: What's the most efficent way to randomize a Queue?

Ok, so say you have a queue with values in it, then you want to randomize the order that they are currently in.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adaykin is offline Offline
73 posts
since Feb 2007
Nov 12th, 2008
0

Re: What's the most efficent way to randomize a Queue?

My point is that its not a queue, then. Just take an ArrayList, use int index = Math.Random(n) where n is the # of items in your ArrayList, then index is the random index you want.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 12th, 2008
0

Re: What's the most efficent way to randomize a Queue?

I think you still don't get it....Say there is a queue with elements a, b and c in that order. I want to know what the best way to put the elements in a random order, so you might save the elements off to an array shuffle them, and then put them back into the queue so the new order in the queue would be b, a, c.

But keep in mind a, b, and c are just arbitrary elements if that helps you understand it....
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adaykin is offline Offline
73 posts
since Feb 2007
Nov 12th, 2008
0

Re: What's the most efficent way to randomize a Queue?

The simplest way would be to use the Collections#shuffle(List<?> list) method which delivers decent performance and output.

If you have an assignment to create such a shuffling algorithm, then look at the source code of the Collections class for hints.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Nov 12th, 2008
0

Re: What's the most efficent way to randomize a Queue?

Yeah collections sounds like a good way to go since the shuffle method is built right in, thanks.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adaykin is offline Offline
73 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Returning string from method help
Next Thread in Java Forum Timeline: please help in Jdbc connectivity





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC