Thread: rand() function giving same number repeatedly

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    rand() function giving same number repeatedly

    Hello everyone. I'm having a bit of a problem getting my probability program to work. It's supposed to determine the probability that 13 will be achieved with 3 dice rolls but every die gets the same value.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int rand_int(int,int);
    int a=1, b=6;
    
    main()
    {
    	int die[100], n=0, i, dietot=0, l;
    	double prob;
    
    	for(i=0;i<100;i++)
    	{
    		die[i]=rand_int(1,6);
    		printf("\nDie %d = %d",i+1,die[i]);
    		dietot=dietot+die[i];
    		if(dietot==13)n++;
    		l=i%3;
    		if(l==0)dietot=0;
    	}
    	prob=n/100;
    	printf("\nThe probability of throwing 3 dice and getting 13 is %lf out of 100",prob);
    }
    
    int rand_int(int a, int b)
    {
    	srand(time(0)); //takes the current system time as a seed number
    	return (a + rand()%(b-a+1));
    }
    I'm really at a loss as to why this code won't work. My seed should give a different random number each time the loop calls rand_int but I just get 100 identical dice...

    Any help would be greatly appreciated!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should just seed the PRNG once. What is probably happening now is that time(0) is being called 100 times, and since all 100 calls happen within the same second, the result of time(0) is the same, thus the first number in the PRNG sequence is the same. If you only seeded the PRNG once, you would actually get to use the first 100 numbers in the sequence.
    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
    Join Date
    Nov 2009
    Posts
    2
    Thanks very much laserlight! Put the srand() in to the main program before the loop and it now works perfectly. I'll have to complain to my lecturer because he defined rand_int like this:

    Code:
    int rand_int(int a,int b)
    {
    srand( (unsigned)time( NULL ) );  
    return rand()%(b-a+1) + a;
    }
    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  5. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM