# Shuffle Reimplemented

• I'm sure every company has someone you know like this. There is one guy who is good a figuring out an algorithm to do something "new" (in the sense that our code doesn't do this yet). You give him a description of the inputs and outputs and he'll write something that works in all cases (with the occasional oversight). Sure, that's pretty low level work, but it's always nice to have someone like this around to help share the load while you go off figuring out how to solve the bigger questions in life.

Unfortunately, said person is so good at implementing algorithms, that he often doesn't bother to ask what might already exist in either the Java libraries or in our own code base. As a result, you end up with chunks of code like this:

``````parts = new int[NUM_PART];
Vector<String> numbers = new Vector();

int index = randomNumberBetween(0, numbers.size()-1);
parts[0] = Integer.parseInt(numbers.elementAt(index));
numbers.removeElementAt(index);

index = randomNumberBetween(0, numbers.size()-1);
parts[1] = Integer.parseInt(numbers.elementAt(index));
numbers.removeElementAt(index);

parts[2] = Integer.parseInt(numbers.elementAt(0));
``````

To give a little context, this is in a game where the developer is trying to shuffle the order of three parts of a page to be displayed to the user. randomNumberBetween() is anonymized here, but it works with the game's random number generator to return a number between min and max, inclusive.

• Nice. It's been a while since I did JAVA, but I presume there's something like `(new int[] { 0, 1, 2 }).OrderBy((a,b) => 1 - randomNumberBetween(0, 1) * 2)`.

• Some odd stuff there that could be better... but at least it's functional? Right?

In response to @cartman82:
[code]
Collections.shuffle({1,2,3});
[/code]
Might need some type-casting in there or whatever, but that is basically how you shuffle an array in java.

• Also, here is a giant SO topic with most people instructing the poster to implement their own shuffle:

and one lone soul telling them PLEASE GOD NO JUST USE THE ONE BUILT-IN.

Judging by the one-boxed pic of the person that asked the question, I'm guessing things ended poorly

• I'm not related to that guy.

• Some odd stuff there that could be better... but at least it's functional? Right?

In response to @cartman82:

Collections.shuffle({1,2,3});

Might need some type-casting in there or whatever, but that is basically how you shuffle an array in java.

@quijibo said they are using some kind of customized random number generator. Not sure if you can make `Collections.shuffle` work with that (in case they need to produce the same sequence of numbers for testing or whatever).

• @quijibo said they are using some kind of customized random number generator. Not sure if you can make Collections.shuffle work with that (in case they need to produce the same sequence of numbers for testing or whatever).

Okay, I simplified those lines a little too much. We still use Java Random objects, we just have extra methods for simple cases that aren't covered (like an int between min and max, but a multiple of another number). So yes, Collections.shuffle(); is the right way to go.

It does suck that you need to write a little extra code to convert between an int[] and a Collection. But writing your own shuffle is not necessary.

Other wtfs include:

1. Creating a Vector<String> instead of Vector<Integer> (Java 1.5+ has auto-boxing to make this easy)
2. As a result of 1, having to parse each int out after shuffling.
3. Creating a new generic Vector and assigning it to a type-specific variable.  (I didn't think that was allowed, but it is a warning in Eclipse at least)

• It's a good example of a person that is very sound logically but not very good with the programming language itself. Most noobs would have implemented something broken - I'd expect a lot of repeating randoms in most nooby solutions, or keep selecting randoms until the random isn't one that was already taken.

• > @quijibo said they are using some kind of customized random number generator. Not sure if you can make Collections.shuffle work with that (in case they need to produce the same sequence of numbers for testing or whatever).

Okay, I simplified those lines a little too much. We still use Java Random objects, we just have extra methods for simple cases that aren't covered (like an int between min and max, but a multiple of another number). So yes, Collections.shuffle(); is the right way to go.

It does suck that you need to write a little extra code to convert between an int[] and a Collection. But writing your own shuffle is not necessary.

Other wtfs include:

1. Creating a Vector<String> instead of Vector<Integer> (Java 1.5+ has auto-boxing to make this easy)
2. As a result of 1, having to parse each int out after shuffling.
3. Creating a new generic Vector and assigning it to a type-specific variable.  (I didn't think that was allowed, but it is a warning in Eclipse at least)

4. Using Vector instead of ArrayList

• Using Vector screams just out of college student that did all their class work in C++.

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.