Random Number Help

This is a discussion on Random Number Help within the C Programming forums, part of the General Programming Boards category; Hello! I'm new to the forum and I just wanted to say hello and get some assistance. I'm currently teaching ...

  1. #1
    neu
    neu is offline
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Unhappy Random Number Help

    Hello! I'm new to the forum and I just wanted to say hello and get some assistance.

    I'm currently teaching myself the C language before starting the class in the fall. I'm in the process of writing a program that prints out 200 random numbers and at the same time count the number of rundom numbers that is printed below RAND_MAX/2.

    Printing out the 200 random numbers was no problem but when it came to counting the random numbers that was below the RAND_MAX/2. It didn't work out as well as I hoped. Can somebody give me some assistance on this. Below is what I came up with.

    #include<stdio.h>
    #include<stdlib.h>

    int main(void)
    {
    int i,n;
    int median = RAND_MAX/2;
    int plus_cnt = 0;
    int sum = 0;

    for (i=0; i<10; ++i){

    if (i%6 == 0)
    printf("\n");
    printf("%10d", rand());

    if( rand()< median)
    sum = sum + 1;
    plus_cnt = plus_cnt + 1;

    }

    printf("\n");
    printf("sum = %d\n", sum);
    return 0;
    }

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    every time you call the rand() function its going to return a different random number, so first set the return of rand() to variable and reference that instead.

    also you can simplify these statements:
    sum = sum + 1;
    plus_cnt = plus_cnt + 1;

    C has an operator for this frequently done task: ++

    ++sum; is the equivilent to doing sum = sum + 1;

    you should look it up, as using it on either side of the variable (post inc and the other one i forget the name..heh) have different meanings.

    one last thing, next time use code tags.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    ++sum; is the equivilent to doing sum = sum + 1;
    Close but not 100% identical.

    When ++sum or sum++ are on their own line it is but if you combine it with another statement such as: printf("%d", sum++) or printf("%d", ++sum) the difference becomes a big deal. I won't give you the answer but lookup pre and post increment. A better subsitution is sum += 1;

    It didn't work out as well as I hoped. Can somebody give me some assistance on this.
    How do the results you expect differ from the results you got?

    Also please read the stick thread at the top of forum to learn how to use the code tags, it aids greatly in readibility of the code on the forum.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Expanding upon nonpuz' reply and your initial effort...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
       int i, median = RAND_MAX / 2, sum = 0;
       printf("median = %d\n", median);
       srand(time(0));
       for ( i = 0; i < 20; ++i )
       {
          int result = rand();
          printf("%10d", result);
          if ( i % 6 == 5 )
          {
             putchar('\n');
          }
          if ( result < median )
          {
             ++sum;
          }
       }
       printf("\nsum = %d\n", sum); 
       return 0;
    }
    
    /* my output
    median = 16383
           995      9630     15624      7358     13419     30050
         17016     14786     16103     17981     13452      4755
         26854      4556     26678     20163     18700     25290
          2696     24808
    sum = 11
    */
    Last edited by Dave_Sinkula; 04-09-2004 at 08:50 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    I believe i stated that just below what you quoted...more or less. But whatever.

  6. #6
    neu
    neu is offline
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    Thank you, all your suggestions and example definitely helped me understand how to work the problem. Next time I'll use the code tags.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 09:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21