10 January 2018, 18:02

# probability and movement

## What is Probability?

Probability is the measure of the likelihood that an event will occur. It is a way to evaluate a given set of scenarios and decide how often will event x happen.

Lets look at an example in code:

### Random Walker

See the Pen Random Walker by Adam Harpur (@harps116) on CodePen.

In the example above we set up our little walker object that starts with an x and a y in the middle of the screen, a number is then choosen between 0 - 3, this number determines the walkers next step.

We could say that the walker has 25% chance of going up, down, left or right. That there is an equal chance of the walker moving in one of four directions.

What if we wanted more control over the way the walker moves?

### Controlling the Random Walker

In this case I want the walker to move biasedly to the right

There are a few different approaches that come to mind here:

• We could increase the conditions for a particular direction

Psuedo Code

``````choice = random(5)

case: n == 0 || 5
this.x++
break;
``````
• We could add more liberal conditions to a particular direction

Psuedo Code

``````choice = random(4)

case: n < 3
this.x++
break;
``````
• We could increase the incrementor for a particular direction

Psuedo Code

``````choice - random(4)

case: n = 0
this.x += 2
break;
``````

As with any given coding endeveour that are many ways to solve a problem, it is up to you to decide the approach that makes the most sense.

In this particular case I like the final approach as it seems the most natural to me and least likely to have side effects on the mechanics of the other code.

See the Pen Random Right Walker by Adam Harpur (@harps116) on CodePen.

That's all I mangaged to tackle today, I hope to continue looking at movement in code this week.

← Make a Decision
Starting ~ 2018 →