Thread: Help with rand()

  1. #16
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by brewbuck View Post
    Then I'd call it like this:

    Code:
    double x = round( RandRange( -100.0, 100.0 ) ) / 100.0;
    I'm not sure, but at first glance I think that produces a bias against both -1.0 and 1.0.

  2. #17
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Maybe
    Code:
    double x = round( RandRange( -100.4999, 100.4999 ) ) / 100.0;
    or whatever number of 9s you like to reduce the bias.

  3. #18
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    Quote Originally Posted by R.Stiltskin View Post
    You're getting integers in the range 0 - 199 so the mean will be 99.5, not 100. Divide by 100, subtract 1 and multiply by 100,000 and your mean *should* be -500. You probably want something like this:

    Code:
    for (int j = 0; j < 10000; j++) {
      double sum = 0;
      double frac = 201.0 / (unsigned int)(RAND_MAX + 1);
      for (int i = 0; i < 100000; i++) {
        double n =  (int)(rand() * frac) / 100.0 - 1;
        sum += n;
      }
    }
    I am disappointed I missed that one. Thanks a lot for all the help to everyone that posted.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    (rand() * 201), on anything more than (RAND_MAX/201) is going to end up with an overflow.


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

  5. #20
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by quzah View Post
    (rand() * 201), on anything more than (RAND_MAX/201) is going to end up with an overflow.
    That's fairly obvious. I was careful to avoid doing that by storing the fraction first.

    There's no overflow here:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main( void )
    {
        int i;
        double n;
        double frac = 201.0 / ((unsigned int)RAND_MAX + 1);
        
        printf( "denom = %u\n", (unsigned int)RAND_MAX + 1 );
        printf( "frac = %e\n", frac);
        
        for( i = 10000000; i > 0 && i < RAND_MAX; i+=10000000 ) {
          n = (int)(i * frac) / 100.0 - 1;
          printf( "%f\n", n);
    //      printf("%f\n", (int)(i * frac) / 100.0 - 1);
        }
        n = (int)(RAND_MAX * frac) / 100.0 - 1;
        printf( "finally %f\n", n );
        return 0;
    }

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by R.Stiltskin View Post
    That's fairly obvious. I was careful to avoid doing that by storing the fraction first.
    My bad, I didn't see the first line.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 10:13 PM