1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Random Walk w/o If-Statements

  1. Apr 17, 2015 #1
    1. The problem statement, all variables and given/known data
    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).

    2. Relevant equations
    steps = number of steps taken in this walk: random (0..5)

    3. 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
    value = (number *a) + b

    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)

  2. jcsd
  3. Apr 17, 2015 #2
    I've continued to tinker and just by using the negative function (-1^number) I can get:
    n=0, v = +1
    n=1, v = -1
    n=2, v = +1
    n=3, v = -1
    n=4, v = +1

    If I could get just a SINGLE one of those to be zero, that would do it. I thought about doing -x,0,+x,-y,0,+y but that gives 0 a 1/3 probability and -x,+x,-y,+y a 1/6 probablitiy each.
  4. Apr 18, 2015 #3


    Staff: Mentor

    What language are you using? That would be important information to know. If you're using a language based on C, there's the conditional operator ( ?: ), which works a lot like an if statement. For example, here's the code to find the larger of two numbers.

    Code (C):
    int x = 3;
    int y = 5;
    int result;

    result = (x >= y) ? x : y;
    If x is greater than or equal to y, result gets set to x's value; otherwise (i.e., if x < y), result gets set to y's value.
  5. Apr 18, 2015 #4
    Use the random index in range 0 to 4 (or 1 to 5 ) to index into an array of directions, [ [0,0],[1,0][0,1],[-1,0],[0,-1] ]
  6. Apr 18, 2015 #5
    Mark44, Duh! That might have been helpful wouldn't it? There are plenty of fall-back methods, but I was trying to do it without a condition statement of any kind (I should have said that but I spent 5 hours in lab yesterday and I was exhausted.

    Gianni-F, YOU ARE MY HERO!! That is ridiculously simple! It is EXACTLY the kind of thing I was looking for. I was hoping for some kind of algebraic tomfoolery but this is much better! Thanks!!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted