[opensource] Random seed

Jim Dinan dinan at cse.ohio-state.edu
Tue Mar 20 19:06:45 EDT 2007

Not exactly.  What your algorithm does is select random elements from
the old array and move them into a new array.  What Darla suggested is
performing random swaps in place on the original array.  One algorithm
for doing this is the Fisher-Yates shuffle (googled at

for (int i = 0; i < deckLength; i++) {
   int r = i + (int) (Math.random() * (deckLength-i));
   swap(deck[r], deck[i]);

Personally, I would use something like this.  If you want to preserve
the original array, you can copy it first, then perform the shuffle.  In
particular, this construction you used:

while (oldDeck[oldPosition].length() == 0) {
  oldPosition = random.nextInt(deckLength-1)+1;

Is not such a good idea since we have no idea how long it will run for.
 In the worst case, that is an infinite loop.  Swapping in place
eliminates the need to search for an element to move since there are
never any empty slots in the array.


> The swap thing you mentioned is, more or less, what my current algorithm does. Thanks anyway though.
> -Brian Swaney
> ----- Original Message -----
> From: Darla Shockley <shockley at cse.ohio-state.edu>
>> By "swap(arr[a],arr[b])", I just meant that as shorthand for:
>> tempString = arr[a];
>> arr[a] = arr[b];
>> arr[b] = tempString;

James Dinan <dinan at cse.ohio-state.edu>

Graduate RA - Computer Science and Engineering
              The Ohio State University

More information about the Opensource mailing list