Created
March 22, 2017 02:06
-
-
Save xylude/adf7743426ac9beecdac9bb0427b244c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class RandomWalk | |
{ | |
public static void main(String[] args) | |
{ | |
Random rand = new Random(); | |
int position = 0; | |
int zeroCrossings = 0; | |
int i = 0; | |
int steps = 0; | |
int timesTired = 0; | |
int timesPlusTen = 0; | |
int timesMinusTen = 0; | |
int timesCrazyError = 0; | |
//wrap in this for loop for step 5 | |
for(i=0; i < 1000; i++) { | |
//resets | |
zeroCrossings = 0; | |
steps = 0; | |
position = 0; | |
while (position != 10 && position != -10 || steps == 50) | |
{ | |
if (rand.nextDouble() < 0.5) | |
{ | |
position--; | |
} | |
else | |
{ | |
position++; | |
} | |
//add this for step 4: | |
if(position == 0) { | |
zeroCrossings++; | |
} | |
steps++; | |
} | |
if(steps == 50) { | |
timesTired++; | |
} else if(position == 10) { | |
timesPlusTen++; | |
} else if(position == -10) { | |
timesMinusTen++; | |
} else { | |
timesCrazyError++; | |
} | |
} | |
System.out.println(); // print newline at end of position list | |
System.out.println("Times +10: "+timesPlusTen); | |
System.out.println("Times -10: "+timesMinusTen); | |
System.out.println("Times 50 steps: "+timesTired); | |
System.out.println("Times ERROR: "+timesCrazyError); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment