Transforming Random Walk Steps into Directional Movement

In summary, Gianni-F found a way to solve the homework equation without using an if-statement. He used the conditional operator ( ?: ) to transform the ranges 0- .20 and .21- .40 into 0- .10 and .11- .20. This solved the problem.
  • #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.
 
Physics news on Phys.org
  • #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.
 
  • #3
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.
 
  • #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] ]
 
  • #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!
 

1. What is a "Random Walk w/o If-Statements"?

A "Random Walk w/o If-Statements" is a type of mathematical simulation that models the random movement of a particle or object without using conditional statements, such as if-else statements. It is often used in fields such as physics, biology, and finance to study the behavior of complex systems.

2. How does a "Random Walk w/o If-Statements" differ from a traditional "Random Walk"?

In a traditional "Random Walk" simulation, the movement of the particle or object is determined by a series of conditional statements, such as if-else statements. In a "Random Walk w/o If-Statements", the movement is determined by a set of predefined rules and probabilities, making it a more efficient and accurate way to model random behavior.

3. What are the advantages of using a "Random Walk w/o If-Statements" over a traditional "Random Walk"?

One advantage is that a "Random Walk w/o If-Statements" is more computationally efficient, as it does not need to evaluate multiple conditional statements for each step. It also allows for more complex and realistic behavior to be modeled, as the rules and probabilities can be customized to fit specific scenarios.

4. What are some applications of "Random Walk w/o If-Statements" in scientific research?

"Random Walk w/o If-Statements" is commonly used in fields such as physics, biology, and finance to study the movement and behavior of particles, molecules, cells, and financial market trends. It can also be applied in computer science for tasks such as path finding and optimization problems.

5. How can I learn more about "Random Walk w/o If-Statements" and its applications?

There are many online resources and scientific articles available that discuss "Random Walk w/o If-Statements" and its applications in various fields. Conducting a literature review or consulting with experts in the field can also provide valuable insights and resources for further learning.

Similar threads

  • Precalculus Mathematics Homework Help
Replies
29
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
892
  • Engineering and Comp Sci Homework Help
Replies
1
Views
972
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
892
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Replies
1
Views
647
Back
Top