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

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

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

 
0
  #1
Nov 12th, 2008
I've seen a lot of different possible ways to do so, but I was wondering what people thought the most efficient way was.
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

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

 
0
  #2
Nov 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

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

 
0
  #3
Nov 12th, 2008
Ok, so say you have a queue with values in it, then you want to randomize the order that they are currently in.
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

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

 
0
  #4
Nov 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

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

 
0
  #5
Nov 12th, 2008
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....
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,623
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #6
Nov 12th, 2008
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

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

 
0
  #7
Nov 12th, 2008
Yeah collections sounds like a good way to go since the shuffle method is built right in, thanks.
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC