KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dso > concurrency > Shuffler


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package dso.concurrency;
5
6 import java.util.Random JavaDoc;
7
8 /**
9  * Created by Alan Brown
10  * Date: May 17, 2005
11  * Time: 1:48:02 PM
12  */

13 public class Shuffler {
14
15     private int shuffleSize = 0;
16     private Random JavaDoc random = new Random JavaDoc();
17     int[] positions;
18
19     public Shuffler(int shuffleSize) {
20         this.shuffleSize = shuffleSize;
21         positions = new int[shuffleSize];
22         for (int i=0; i<shuffleSize; i++) {
23             positions[i] = i;
24         }
25     }
26
27     public int[] shuffleOrder() {
28
29         for (int i=0; i<shuffleSize; i++) {
30             int changingPosition = random.nextInt(shuffleSize);
31             int swappedValue = positions[i];
32             positions[i] = positions[changingPosition];
33             positions[changingPosition] = swappedValue;
34         }
35         return positions;
36     }
37 }
38
Popular Tags