- #1
Rapier
- 87
- 0
Homework Statement
The problem is relatively simple. I am modelling mass diffusion, and that's going well, but she has offered extra credit if we can write the problem without using any if-statements. In a clutch my plan is fall back on a Case Statement and pull barracks-lawyer and insist that I've met the requirements. 8P I have a plan, but I'm having a problem (see below).
Homework Equations
steps = number of steps taken in this walk: random (0..5)
The Attempt at a Solution
This was my initial pseudocode.
number = random(0..1) * 5
if number = 1, position = x-1, y
if number = 2, position = x+1,y
if number = 3 position = x, y-1
if number = 4 position = x,y+1
if number = 5 position = x,y
I originally had number = random (0..1) and was using 0.20 increments, but integers seemed easier.
I have been thinking along the lines of below. My thought was that number (in this case) must go from 0..2. If the value is 0 then the walker moves to the left, -1 + 0 = -1. If value is 1 then the walker stays, -1 + 1 = 0. If the value is 2 then the walker moves to the right, -1 + 2 = 1.
if (number > 0 and number < 3) value = steps * (-1 + number)
position = x, y + value
While this at first seemed to have promise without using an if-statement I can't think of a method to exclude the values 3 and 4, because -1 + 4 = +3. I want to limit the results to either -1, 0 or 1 to determine x direction. Then I want to do something similar value for the y direction. I also only want the walker moving in a single direction. I don't want them moving horizontally AND vertically in the same steps.
I've been trying to come up with some algebraic relationship to do this similar to how we define odd/even:
Odd = (2n - 1)
Even = 2n
I've tried coming up with something that would fall roughly into the same kind of linear pattern. I've been wondering if the 'rounding' trick might not be applicable. The Rounding "Trick" is trunc(number + 1/2).
value = (number + a) * b
or
value = (number *a) + b
etc.I'm looking for suggestions on a way to transform these ranges:
0 - .20 = -1 (left, -x direction)
.21 - .40 = +1 (right, +x direction)
.41 - .60 = -1 (down, -y direction)
.61 - .80 = +1 (up, +y direction)
.81 - 1.0 = 0 (no movement)
Thanks.