Thread: Random Walk

  1. #1
    Registered User hadi0x7c7's Avatar
    Join Date
    Jun 2010
    Location
    IRI
    Posts
    8

    Question 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;
    }
    Last edited by hadi0x7c7; 08-01-2012 at 11:46 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What makes you think that there is something wrong with that code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User hadi0x7c7's Avatar
    Join Date
    Jun 2010
    Location
    IRI
    Posts
    8
    the output is just an array of '.' !

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hadi0x7c7
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User hadi0x7c7's Avatar
    Join Date
    Jun 2010
    Location
    IRI
    Posts
    8
    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. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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. #8
    Registered User hadi0x7c7's Avatar
    Join Date
    Jun 2010
    Location
    IRI
    Posts
    8
    Quote Originally Posted by ZuK View Post
    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. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Shure. It just has to be somebody elses fault.
    Kurt

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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?
    Last edited by iMalc; 08-01-2012 at 01:16 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User hadi0x7c7's Avatar
    Join Date
    Jun 2010
    Location
    IRI
    Posts
    8
    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 !
    thanks for your attention !
    Last edited by hadi0x7c7; 08-01-2012 at 03:43 PM.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    Registered User hadi0x7c7's Avatar
    Join Date
    Jun 2010
    Location
    IRI
    Posts
    8
    Quote Originally Posted by oogabooga View Post

    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 !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Walk Hexogonal lattice
    By SalamuB in forum C Programming
    Replies: 3
    Last Post: 02-13-2012, 01:44 PM
  2. Random Walk Simulation in 1D for 10 or more particles
    By SalamuB in forum C Programming
    Replies: 1
    Last Post: 02-06-2012, 12:48 PM
  3. Random walk solution
    By skiabox in forum C Programming
    Replies: 5
    Last Post: 11-01-2010, 10:36 AM
  4. Code for random walk in 1D
    By LLcoolc++ in forum C Programming
    Replies: 5
    Last Post: 10-18-2009, 09:33 AM
  5. Help with a random walk
    By pxleyes in forum C Programming
    Replies: 11
    Last Post: 02-27-2004, 09:11 PM