Transforming Random Walk Steps into Directional Movement

Click For Summary

Discussion Overview

The discussion revolves around modeling mass diffusion through a random walk while adhering to constraints that prohibit the use of if-statements. Participants explore various methods to transform random walk steps into directional movement without conditional statements, focusing on pseudocode and mathematical relationships.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation

Main Points Raised

  • One participant describes their initial pseudocode for a random walk and expresses difficulty in avoiding if-statements while limiting movement to specific directions.
  • Another participant suggests using the negative function to alternate values between +1 and -1, seeking a way to include a zero value for no movement.
  • A participant inquires about the programming language being used, noting that some languages have conditional operators that may serve a similar purpose to if-statements.
  • Another suggestion involves using a random index to select from an array of predefined directional movements, potentially simplifying the implementation.
  • A participant expresses appreciation for the array method as a straightforward solution, contrasting it with their earlier, more complex algebraic attempts.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single method to achieve the desired outcome without using if-statements, as multiple approaches are proposed and explored.

Contextual Notes

Participants express uncertainty about how to effectively implement their ideas without conditional statements, and there are unresolved questions about the best mathematical transformations to achieve the desired directional movement.

Who May Find This Useful

Readers interested in programming, algorithm design, or mathematical modeling in the context of random walks and diffusion processes may find this discussion relevant.

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

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
29
Views
2K
  • · Replies 3 ·
Replies
3
Views
790
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K