d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Random Walking Program
Add Reply New Topic New Poll
Member
Posts: 20,319
Joined: Jun 23 2007
Gold: 3.01
Nov 7 2013 11:10pm
I get the following info from user
-Grid size
-Number of steps
-Percent chance of walking up, down, left and right (totally 100%)


Need some help setting this up,
So first get the info from them, then I would do a for loop for total number of steps they gave me, generate random up,down,left,right.

When they hit a boundary, the walking stops.
This is the code I was given. How do i find the random number with percentage changes they gave?

Believe I am suppose to set 3 boundary meeting functions


r = random();
if(r < Left) {
x = x-1;
} else if(r <Left+Right) {
x = x+1;
} else if(r<Left+Right+Up) {
y = y+1;
} else {
y = y-1;
}



e/ He says to have 3 random walking boundry functions (they give us up,left, right percentage values, we assume its down if its not one of the three)

This post was edited by gramkracka22 on Nov 7 2013 11:24pm
Member
Posts: 20,319
Joined: Jun 23 2007
Gold: 3.01
Nov 7 2013 11:31pm
I c, to get the random number just generate one between 1 and 100 then compare it to the up,down,left and right percentages.



still not sure where I incorporate these boundary checking functions in

This post was edited by gramkracka22 on Nov 7 2013 11:34pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 7 2013 11:40pm
if x > x_max...
if x < x_min....
etc
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Nov 8 2013 08:39am
for(int i = 0 ; i < totalSteps; i++){

r = random();
if(r < Left) {
x = x-1;
} else if(r <Left+Right) {
x = x+1;
} else if(r<Left+Right+Up) {
y = y+1;
} else {
y = y-1;
}

if(x>=maxX || x<= minX || y>=maxY || y<=minY){
break;
}
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll