1. ## Random Walk

what's wrong with this code ?
Code:
```// Random walk

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void set();
void print();
void generate();
int possible(int, int);

char a[10][10];

int main()
{

set();
generate();
print();

return 0;
}

void set()
{
int i=0;
int j = 0;

for(i = 0; i < 10; i++)
for(j = 0; j < 10; j++)
a[i][j] = '.';
}

void print()
{
int i,j;

for(i = 0; i < 10; i++){
for(j = 0; j < 10; j++)
printf("%c ", a[i][j]);
printf("\n");
}
}

void generate()
{
int m,n;
int next;
int step = 'A';
m = n = 0;
a[m][n] = step;

while(1){
srand((unsigned) time(NULL));
next = rand() % 4;
// printf("%d\n", next);
step += 1;

if(next == 0 && possible(m + 1, n)){ // move to right
m += 1;
a[m][n] = step;

} else if(n == 1 && possible(m, n+1)){ // move down
n += 1;
a[m][n] = step;
} else if(n == 2 && possible(m - 1, n)){ //move left
m -= 1;
a[m][n] = step;
} else if(n == 3 && possible(m, n-1)){ // move up
n -= 1;
a[m][n] = step;
}else
break;
}
}

int possible(int m, int n)
{
return m >= 0 && m <= 9 && n >=0 && n <= 9;
}```

2. What makes you think that there is something wrong with that code?

3. the output is just an array of '.' !

4. Well the srand() inside the loop is a killer, if you're expecting some kind of random numbers.

Call srand() ONCE ONLY, at the start of main.

the output is just an array of '.' !
Sounds fine to me!

If you don't agree, then you should give us an overview of what the code is supposed to do, tell us what is the expected output and what is the actual output (which you have provided, but only vaguely).

6. its a program to simulate a rondom walk.

for a 4x4 it would print like this:
A N M L
B C J K
. D I H
. E F G

7. i would imagine that
Code:
`    } else if(n == 1 && possible(m, n+1)){ // move down`
should be
Code:
`    } else if(next == 1 && possible(m, n+1)){ // move down`
same for the other directions
Kurt

8. Originally Posted by ZuK
i would imagine that
Code:
`    } else if(n == 1 && possible(m, n+1)){ // move down`
should be
Code:
`    } else if(next == 1 && possible(m, n+1)){ // move down`
same for the other directions
Kurt
this is fault of emacs ;D

9. Shure. It just has to be somebody elses fault.
Kurt

10. Have you made the vital change of moving the srand call as Salem said in post #4?

We're not interested in whether using the wrong variable is a fault of something else or not, the important thing is for you to tell us that you've fixed it and then tell us what results you get after that.

Other than that, you start in the top left and the moment you walk off the edge you stop. So given that there is a 50% chance that your first step is up or left, then 50% of the time you're going to have an empty board. Perhaps you had slightly different logic in mind?

11. after some modification i ended up in this:
I can only take 26 steps [A-Z] and this prorgram should stop at most in 26 steps.
Code:
```// Random walk

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

char a[10][10];

void print()
{
int i,j;
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
printf("%c ", a[i][j]);
printf("\n");
}
}

void set()
{
int i,j;

for(i = 0; i < 10; i++)
for(j = 0; j < 10; j++)
a[i][j] = '.';
}

int in_bound(int m, int n)
{
if(m >= 0 && m <= 9 && n >= 0 && n <= 9)
return 1;
else
return 0;
}

int not_used(int m, int n)
{
if(a[m][n] != '.')
return 0;
else
return 1;
}

void gen()
{
int m,n;
int next_move;

m = n = 0;
char c = 'A';
a[m][n] = 'A';
for(c = 'B'; c <= 'Z'; c++){
int ok = 0;
next_move = rand() % 4;
if(next_move <= 0 && in_bound(m, n-1) && not_used(m,n-1)) {
ok = 1;
n--;
a[m][n] = c;
continue;
}
if(next_move <= 1 && in_bound(m, n+1) && not_used(m, n+1)) {
ok = 1;
n++;
a[m][n] = c;
continue;
}
if(next_move <= 2 && in_bound(m-1, n) && not_used(m-1,n)) {
ok = 1;
m--;
a[m][n] = c;
continue;
}
if(next_move <= 3 && in_bound(m+1, n) && not_used(m+1,n)) {
ok = 1;
m++;
a[m][n] = c;
continue;
}if(!ok)
break;
}
}

int main()
{
srand((unsigned)time(NULL));
set();
gen();
print();

return 0;
}```
I just quit this problem maybe tomarrow i tackle it again !

12. Your algorithm breaks early sometimes, even when it has a legitimate move. Look at your logic.

If next_move is 0, then if that direction is out_of_bounds or occupied then it will try the next if condition, and so on. But if next_move is 3, it will only try that direction and not the others. And when next_move is 1 or 2 there's a similar problem: not all directions are tried.

This is a little tricky. You want to try all directions, but you want to try them in a random order, taking the first one that is open.

13. Originally Posted by oogabooga

If next_move is 0, then if that direction is out_of_bounds or occupied then it will try the next if condition, and so on. But if next_move is 3, it will only try that direction and not the others. And when next_move is 1 or 2 there's a similar problem: not all directions are tried.

you'r right thats why it behaves incorrectly !
Its more complex than it seems !