A random walk is a stochastic process in which a particle moves one step at a time from state to state in a structured space. For us, the state space will be Z, the set of integers. The particle starts in an initial state S[0] ∈ Z. If, after i ≥ 0 steps, the particle is in state S[i], then in step i + 1, it moves to state S[i] + 1 with probability p and to state S[i] − 1 with probability q; it cannot stand still. Of course, p + q = 1. If S[0] = 5 and 0 < p < 1, then the sequence 5,4,3,4,3,2,3,2,3,4 is a possible sequence of states for the particle if it moves 9 times.
Write a program that will simulate a random walk for a given number of steps and that will compute certain statistics for the random walk. The parameters for a simulation come from standard input as a single line of parameters, consisting of (1) the initial state S[0]; (2) the value of p; and (3) the number of steps to simulate.
Note: I'm writing this in Java. So far I have: public class RandomWalk
{
//This is the main method that runs the program
@SuppressWarnings("resource")
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[]numbers = new int[3];
//This is the for loop that parses
for (int i = 0; i < 3; i++)
{
System.out.println("Enter a number");
numbers[i] = Integer.parseInt(input.nextLine());
}
Arrays.sort(numbers);
//This is the command prompt to display
System.out.println("Minimum number is " + numbers[0]);
System.out.println("Average number is " + numbers[1]);
System.out.println("Maximum number is " + numbers[2]);
}
}
I need to adjust parts of my code to make it work but not sure how. I'm not sure how to implement a random number generator as per the instructions.
Aucun commentaire:
Enregistrer un commentaire