T
T
Torna2015-01-06 03:33:06
PHP
Torna, 2015-01-06 03:33:06

How to correctly implement a random walk algorithm?

Where can I see the pseudocode or implementation of a random walk? I can't find a solution for the second step.

$steps = 50;
$number = 300;
for ($i = 0 ;$i <= $steps; $i++){
    if ($number <= 0)
       $newNumber = $number + rand(0,100);
    else
     $newNumber = $number - rand(0,100);
  echo $i . " = " . $newNumber  . "\r\n";
}
?>

It is necessary at step 2 and further, the result of the previous step in the $newNumber variable was entered into $number.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Torna, 2015-01-06
@Torn

The assignment method doesn't work, I don't know why.
produces strange result

0
300
0 = 234
234
300
1 = 277
277
300

Found working code
$steps = 50;
$newNumber = 300;

for ($i = 0 ;$i <= $steps; $i++){
  echo $newNumber . "\r\n";
    if ( $newNumber <= 0 )
       $newNumber = $newNumber + rand(0,100);
    else
     $newNumber = $newNumber - rand(0,100);
  echo $i . " = " . $newNumber  . "\r\n";
}
?>

Thank you.

X
xmoonlight, 2015-01-06
@xmoonlight

after echo:
$number=$newNumber;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question