/Users/lyon/j4p/src/bookExamples/ch26Graphics/carl/logPolar/ShuffleTest.java

1    package bookExamples.ch26Graphics.carl.logPolar; 
2     
3    public class ShuffleTest { 
4        public int myArray[]; 
5        int rayCount; 
6     
7        public ShuffleTest() { 
8            rayCount = 10; 
9            System.out.println(" Constructor"); 
10           myArray = new int[rayCount]; 
11           for (int i = 0; i < myArray.length; i++) { 
12               myArray[i] = i; 
13           } 
14       } 
15    
16       public String toString() { 
17           String str = ""; 
18           for (int i = 0; i < myArray.length; i++) 
19               str += "   " + myArray[i]; 
20           return str; 
21    
22       } 
23    
24       public void shuffleRays(int m) { 
25           int k; 
26           int sum = 0; 
27           int holdColor, holdColor2; 
28           int counter = 0; 
29           for (int i = 0; i < rayCount; i += m) { 
30               counter++; 
31               holdColor = myArray[i]; 
32               k = m * (((int) (Math.random() * (float) rayCount)) / m); 
33               sum += k; 
34               holdColor2 = myArray[k]; 
35               for (int j = 0; j < m; j++) { 
36                   if ((i + j) < rayCount) myArray[i + j] = holdColor2; 
37                   if ((k + j) < rayCount) myArray[k + j] = holdColor; 
38               } 
39               System.out.println(m + "  i= " + i + ";  k= " + k + "; " + toString()); 
40           } 
41    
42           System.out.println(" avg k:" + (float) sum / (float) counter); 
43       } 
44    
45       public static void main(String args[]) { 
46           System.out.println("main"); 
47           ShuffleTest shuf = new ShuffleTest(); 
48           System.out.println(shuf.rayCount); 
49           System.out.println(shuf.toString()); 
50           shuf.shuffleRays(7); 
51           System.out.println(shuf.toString()); 
52    
53       } 
54    
55   }