KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > example > conventions > RandomWalk


1 package net.innig.macker.example.conventions;
2
3 import java.util.*;
4
5 public final class RandomWalk
6     {
7     public static void walk(Tree tree, int hops)
8         {
9         while(hops-- > 0)
10             {
11             System.out.println("hop: " + tree);
12             if(tree.getParent() != null && rand.nextInt(4) == 0)
13                 tree = tree.getParent();
14             else
15                 {
16                 List children = new ArrayList(tree.getChildren());
17                 if(children.isEmpty())
18                     tree = tree.getParent();
19                 else
20                     tree = (Tree) children.get(rand.nextInt(children.size()));
21                 }
22             if(tree == null)
23                 return;
24             }
25         }
26     
27     private static Random rand = new Random();
28     
29     private RandomWalk() { } // In the future, Macker will be able to require this, too
30
}
Popular Tags