Thread: Random number generator

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    Random number generator

    Ok I seem to have a further problem, with the rand function. It produces the same set of random numbers in the same order every time it get's called. Now if I use the srand(time(NULL)) to create a different seed every time. The time function seems to return the same number every time and therefore produce the same random number every time.

    Code:
    #include <stdlib.h>
    char* LB_Random(char *maxR,char *dummy)
    	{	
    	int i;
    	static char n[10];
    	int rMax = atoi(maxR);	
    	rMax++;
    	i = rand() % rMax;
    	sprintf(n,"%d",i);
    	return n;	
    	}
    The above produces: 9,3,14,4,1,12,6,14 everytime

    Code:
    #include <stdlib.h>
    char* LB_Random(char *maxR,char *dummy)
    	{	
    	int i;
    	static char n[10];
    	int rMax = atoi(maxR);	
    	rMax++;
                    srand(time(NULL));
    	i = rand() % rMax;
    	sprintf(n,"%d",i);
    	return n;	
    	}
    Produces the number 5 everytime, due to the time function returning the same number.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well here - http://cboard.cprogramming.com/showthread.php?t=85926
    You first got it wrong, how to do it right was explained and then you got it right yourself.

    Now you've regressed back into doing it wrong again.

    > srand(time(NULL));
    This line goes at the start of main.
    You do it exactly once in the program.
    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.

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    srand() should only be called once. Do it outside the function or keep a static boolean variable that knows whether or not srand() has been called.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    Ok let me try and explain this a bit better. I'm using a piece of software which will take a C header file and an obj file, import these as extra functionality and then it can call the functions these have defined so it's something like this.

    Code:
    INCLUDE(random)
    while(not_eof)
    {
        call(random,"15")
        go to next line
    }
    The 15 represents the maximum random number, now my header file looks like this

    Code:
    extern char* LB_Random(char*,char*);
    And as you know my main code looks like this
    Code:
    char* LB_Random(char *maxR,char *dummy)
    	{	
    	int i;
    	static char n[10];
    	int rMax = atoi(maxR);	
    	rMax++;	
    	i = rand() % rMax;
    	sprintf(n,"%d",i);
    	return n;	
    	}
    Seeing as you say I can only call srand once ever, what should I do. The only thing i can think of is defining another function in the above code that seeds during initialisation, and then calling the random function over and over. So something like

    Code:
    //Init
    INCLUDE(random)
    call(seed,"")
    while(not_eof)
    {
       call(random,"15")
       go to next line
    }
    How's that sound.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Looks good.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    Yay it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 06:38 AM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM