Transforming Random Walk Steps into Directional Movement

AI Thread Summary
The discussion revolves around modeling mass diffusion without using if-statements, focusing on transforming random walk steps into directional movement. The original pseudocode attempts to assign positions based on random numbers but struggles with excluding certain values to limit movement to specific directions. Suggestions include using algebraic relationships and the conditional operator in C-like languages to simplify the logic. A breakthrough solution involves indexing into an array of directional coordinates, eliminating the need for conditional statements altogether. The conversation highlights the importance of creative problem-solving in programming challenges.
Rapier
Messages
83
Reaction score
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.
 
Physics news on Phys.org
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.
 
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.

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.
 
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] ]
 
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!
 

Similar threads

Back
Top