DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   What's the most efficent way to randomize a Queue? (http://www.daniweb.com/forums/thread156958.html)

adaykin Nov 12th, 2008 8:14 pm
What's the most efficent way to randomize a Queue?
 
I've seen a lot of different possible ways to do so, but I was wondering what people thought the most efficient way was.

BestJewSinceJC Nov 12th, 2008 8:17 pm
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.

adaykin Nov 12th, 2008 8:38 pm
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.

BestJewSinceJC Nov 12th, 2008 8:52 pm
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.

adaykin Nov 12th, 2008 10:53 pm
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....

~s.o.s~ Nov 12th, 2008 11:09 pm
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.

adaykin Nov 12th, 2008 11:20 pm
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.


All times are GMT -4. The time now is 5:39 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC