Thread: problem with my srand programm

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Unhappy problem with my srand programm

    Question.Write a program that simulates coin tossing.For each toss of the coin the program should print heads or tails.Let the program toss the coin 100 times, and count the number of time each side of the coin appears.Print the result.The program should call a seperate function cointoss that takes no argument and returns 0 for tails and return 1 for head.



    Well my code it aint workin' it keeps on printing heads or talles hundredc time.that when it is executed each time.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    
    
    int cointoss ( void ); //fucntion prototype
    
    int main()
    {
     int sum, cnt;
          srand (time(0));
          sum = cointoss ();
    
    
     for ( cnt =1; cnt <= 100; cnt ++){
         if ( sum == 0 )
         printf ("Heads\t");
         else
             if ( sum == 1 )
    	 printf ("Tails\t");      }
    
    
          getch();
          return 0;
    }
    
    int cointoss ( void )
    
    {
        int toss;
        toss = 1+rand() % 2;
    
        if ( toss == 1 )
        return (0);
        else
            if ( toss == 2 )
            return (1);
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: problem with my srand programm

    Originally posted by datainjector
    Well my code it aint workin' it keeps on printing heads or talles hundredc time.that when it is executed each time.
    This is your main loop. Tell me, how many times are you calling the cointoss() function within this loop?
    Code:
    for (cnt = 1; cnt <= 100; cnt++)
    {
        if (sum == 0)
            printf("Heads\t");
        else if (sum == 1)
            printf("Tails\t");
    }
    Just in case you don't get the answer, I'll tell you. It's none, you're not calling cointoss(), you're printing the value from the first call of cointoss() 100 times over.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    1) You're not calling your cointoss function anywhere in the loop, so the same result is printed 100 times.

    2) You don't keep track of the totals for both heads and tails.

    3) You're overcomplicating the cointoss function and using a less than optimal call to rand. Remember that using rand() % N causes not very random sequences.

    Here's another way to do it:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int cointoss ( void );
    
    int main(void)
    {
      int heads = 0,
          tails = 0,
          cnt;
      srand ((unsigned)time(NULL));
    
      for ( cnt = 0; cnt < 100; cnt++){
        if ( cointoss() != 0 ) {
          printf ("Heads\t");
          heads++;
        }
        else {
          printf ("Tails\t");
          tails++;
        }
      }
      printf ( "Heads: %d\nTails: %d\n", heads, tails );
      getchar();
      return 0;
    }
    
    int cointoss ( void )
    {
      return rand() < RAND_MAX / 2;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Talking i did it

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    
    
    int cointoss ( void ); //fucntion prototype
    
    int main()
    {
     int sum,cnt, head=0, tails =0;
       srand (time(0));
    
    
       for ( cnt =1; cnt <= 100; cnt ++ ){
         if ( cointoss() == 0 ) {
            printf ("Heads\t");
            head++;              }
         else
             {
    	        printf ("Tails\t");
                    tails++;              } }
    
                    puts (" ");
                    printf ("Heads is %d, tails is %d", head, tails );
    
    
          getch();
          return 0;
    }
    
    int cointoss ( void )
    
    {
        int toss;
           toss = 1+rand() % 2;
    
            if ( toss == 1 )
        return (0);
        else
            if ( toss == 2 )
            return (1);
    
    }
    Now i know what was wrong with the program.Thanks to u guys..The first problem was i said sum = cointoss(); and used it in the loop..which was wrong coz it will be counting the same thing 100 times..and ya i said

    if ( cointoss()= 0 )
    printf ("Heads");
    else
    IF ( cointoss() == 1 )
    printf ("Tails");

    well it should print the stuff 100 times right..but the if else stament was wrong because it would count say tails 20 and heads 50 but thats
    70 ..so i removed the else if statment and replacet it with else which finally.worked

    Well u guys dint c that mistake

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Well u guys dint c that mistake
    My code corrected it, I just didn't feel it needed mentioning. And please read my post again on using rand() % N. I go over this every time someone mentions random numbers, but it's like nobody listens.

    -Prelude
    My best code is written with the delete key.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: i did it

    Originally posted by datainjector
    and ya i said

    if ( cointoss()= 0 )
    printf ("Heads");
    else
    IF ( cointoss() == 1 )
    printf ("Tails");

    well it should print the stuff 100 times right..but the if else stament was wrong because it would count say tails 20 and heads 50 but thats
    70 ..so i removed the else if statment and replacet it with else which finally.worked

    Well u guys dint c that mistake [/B]
    We "didn't see that mistake" because it is not in your original code!

    The real reason that code would be wrong is because you're actually calling the function twice, getting two potentially different results from the call.

    This is why that'd be wrong. Again, the reason "we didn't see it", is because that code snipped is not in your orignial code.

    And you are wrong. They were both right. Your code example didn't call the coin toss function inside the loop.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    >>Remember that using rand() % N causes not very
    >>random sequences

    Excuse me but how do you measure randomness ????

    Especialy in this case ... where N is 2 ???

  8. #8
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    >> but it's like nobody listens.
    Awww I listen to ya
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  9. #9
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by borko_b
    >>Remember that using rand() % N causes not very
    >>random sequences

    Excuse me but how do you measure randomness ????

    Especialy in this case ... where N is 2 ???
    One way would be to see the distribution of the sequence
    Assuming , rand() is implemented really well (highly unlikely) well and generates uniformly on the set of integers {0,1,2,..........,RAND_MAX-1}
    then
    rand()%N, will generate a random variable with the following distributuion ,
    it will take values 0,1,2,...,N-1 with the probability
    ([(RAND_MAX-1)/N]+1)/RAND_MAX,([(RAND_MAX-2)/N]+1)/RAND_MAX,............., [(RAND_MAX-N)/N]+1)/RAND_MAX

    If N is very small compared to RAND_MAX all are close to 1/N
    o.w there can be a problem
    if your N is say (RAND_MAX-1)/2 then
    the probability of
    rand()%N being 0 is 3/RAND_MAX rather than 1/N (=2/(RAND_MAX-1) ) which means it will appear approx 1.5 times more than it should .

    There might be other problems depending on how rand() is actually implemented .

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    One way would be to see the distribution of the sequence
    Assuming , rand() is implemented really well (highly unlikely) well and generates uniformly on the set of integers {0,1,2,..........,RAND_MAX-1}
    then
    rand()%N, will generate a random variable with the following distributuion ,
    it will take values 0,1,2,...,N-1 with the probability
    ([(RAND_MAX-1)/N]+1)/RAND_MAX,([(RAND_MAX-2)/N]+1)/RAND_MAX,............., [(RAND_MAX-N)/N]+1)/RAND_MAX

    If N is very small compared to RAND_MAX all are close to 1/N
    o.w there can be a problem
    if your N is say (RAND_MAX-1)/2 then
    the probability of
    rand()%N being 0 is 3/RAND_MAX rather than 1/N (=2/(RAND_MAX-1) ) which means it will appear approx 1.5 times more than it should .
    A well thought out and detailed explanation. I usually don't bother and just say Check Eskimo.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    >>...One way would be to see the distribution of the sequence ...

    Well...

    Nice explanation !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM