Cristiano Magro
2025-10-05 12a2fa369bebf06753654ae56d607465419b0553
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class RandomWalkers {
    public static void main(String[] args) {
 
        int limite = Integer.parseInt(args[0]);
        int cicli = Integer.parseInt(args[1]);
        int totalePassi = 0;
        for (int i = 0; i < cicli; i++) {
 
            int x = 0;
            int y = 0;
            int passi = 0;
 
            while (Math.abs(x) + Math.abs(y) < limite) {
                double r = Math.random();
                if (r < 0.25) {
                    x++;
                } else if (r < 0.5) {
                    x--;
                } else if (r < 0.75) {
                    y++;
                } else {
                    y--;
                }
                passi++;
            }
 
            totalePassi += passi;
        }
 
        System.out.println("average number of steps = " + ((double) totalePassi / cicli));
    }
 
}