Thread: Code for random walk in 1D

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Code for random walk in 1D

    hi,

    Im trying to make a file with the results of the final position of 1000 1d random walks printed on it but at the moment my file seems to give the same random value 1000 times, can anyone see anyproblems with my code??


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    #define N 1000
    
    
    double rndm(void)
    	{
    		return(double)rand()/((double)RAND_MAX);
    	}
    	
    	
    /*Function to simulate random walk in 1D*/
    	
    double walk(int start) 
    	{
    	
    	double a;
    	int i,x;
    	srand(time(NULL));
    	x=start;
    		
    	for(i=0; i<N; i++) 
    		
    		{
    		
    		a=rndm();
    		if(a<0.5) {    /*Choses which direction to move in*/
    		(x=x+1);
    		}
    	        if (a>0.5) {
    		(x=x-1);
    		}   
    		}
    		return x; 	
    		}
    		
    /*Main Function*/
    	
    int main () {
    	FILE *fout;
    	int i,start,random[N],x;
    	srand(time(NULL));
    	fout=fopen("walk.txt", "w");   //Open The File
    	
    	
    	printf("What would you like the initial x position to be?\n:");    
    	scanf("%d", &start);						//Define Start position for x 
    		
    	for(i=0; i<N; i++)
    		{
    		x=walk(start);		 // Generates Random Number
    		random[i]=x;      
    		fprintf(fout, "%d\n", random[i]);   // Writes Number To File
    		}
    	fclose(fout);                  // Closes The File
    	return 0; 
    		
    }
    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Have you printed up the values being given to double a, by rndm() ?
    Last edited by Adak; 10-18-2009 at 08:52 AM.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    You're calling srand everytime you call walk, you should only call srand ONCE as you do in main and keep it at that.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Adak View Post
    I'm wondering if the division is being done BEFORE the cast to a double. When you're dealing with precedence of operaters in C
    That is definitely not the case. BTW you only need to cast the expression, not the individual numbers:
    Code:
    return (double)rand()/RAND_MAX;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're too quick, MK! That's why I edited that part out.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    That is definitely not the case. BTW you only need to cast the expression, not the individual numbers:
    Code:
    return (double)rand()/RAND_MAX;
    If you only cast the expression, the division is done as an integer, and you will always get 0. Fortunately in your example, you are not casting the expression, but the result from rand(), and it is enough to cast just one of the operands.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Trying to make this code faster & Cramer
    By just2peachy in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2004, 10:54 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM