32 lines
1019 B
Java
32 lines
1019 B
Java
public class RandomWalkers
|
|
{
|
|
public static void main(String[] args)
|
|
{
|
|
int r = Integer.parseInt(args[0]);
|
|
int trials = Integer.parseInt(args[1]);
|
|
|
|
int xCoordinate = 0;
|
|
int yCoordinate = 0;
|
|
int steps = 0;
|
|
double average = 0;
|
|
|
|
//System.out.println("(" + xCoordinate + ", " + yCoordinate + ")" );
|
|
|
|
for(int i = 1; i <= trials; i++)
|
|
{
|
|
while(Math.abs(xCoordinate) + Math.abs(yCoordinate) < r)
|
|
{
|
|
if(Math.random() <= 0.25) xCoordinate += 1;
|
|
else if (Math.random() <= 0.50) xCoordinate -= 1;
|
|
else if (Math.random() <= 0.75) yCoordinate += 1;
|
|
else yCoordinate -= 1;
|
|
|
|
//System.out.println("(" + xCoordinate + ", " + yCoordinate + ")" );
|
|
steps++;
|
|
}
|
|
}
|
|
average = (float) steps / trials;
|
|
System.out.println("average number of steps = " + average);
|
|
}
|
|
}
|