random min -0.5 to 0.5

This is a discussion on random min -0.5 to 0.5 within the C Programming forums, part of the General Programming Boards category; Hi there could someone explain Code: rc= rand()% (max-min+1)+min ; because I do not understand the term after modulus %. ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    22

    random min -0.5 to 0.5

    Hi there

    could someone explain
    Code:
    rc= rand()% (max-min+1)+min ;
    because I do not understand the term after modulus %.
    I got this code from the homepage and was wondering how to use it with random double numbers.
    example: min -1.0 max 1.0 . result -0.985, -0.750, 0.552...
    any hint?

    thx!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int getrand(int, int);
    
    int main (void)
    {
       int i;
       int r;
       printf("\nrandom numbers\n");
       for(i=0; i<20;i++)
       {
          r= getrand(-5.,5.);
    	  printf("your number is %d\n", r);
       }
       return 0;
    }
    
    int getrand(int min, int max)
    {
       static int Init=0;
       double rc;
       if (Init==0)
       {
          srand(time(NULL));
          Init=1;
       }
       rc= rand()% (max-min+1)+min ;
       return (rc);
    }
    Last edited by staticalloc; 09-28-2004 at 05:17 PM.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Well generate a range using the modulus operator, then divide to get the numbers you want.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    The function shown in your listing generates "random" integers that are greater than or equal to min and less than or equal to max.

    Here's the idea:

    The standard C library function rand() generates a sequence of integers between 0 and some large value. (The largest value that rand() can generate for a given compiler is given by a constant RAND_MAX, which is defined in <stdlib.h>.) Every time you call rand() you get another number between 0 and RAND_MAX.

    Now for non-negative integers, the % operator gives the remainder that is obtained by dividing the first number by the second. So, if you take any int, say Z, and let x = Z % 11, the result in x will always be greater than or equal to zero and less than or equal to 10. (That's how division works: the remainder is greater than or equal to zero and less than the divisor.) This program assumes that the remainders will be "random" also (since they were obtained by dividing a "random" number by a constant).

    So if we want to obtain a random number between -5 and 5, we see that there are 11 possible values of integers (count them: -5, -4, ..., 4, 5). The number if integer values between min and max is given by (max - min) + 1.

    In other words the value x in the following expression is greater than equal to 0 and less than or equal to 10 (where min = -5, max = 5):

    Code:
    x = rand() % (max - min + 1);
    Then, since we want to find a "random" integer number between min and max, one way is to use the formula
    Code:
    x = rand() % (max - min + 1) + min;
    Note that, although your example calls getrand(5.,5.) the arguments are converted to ints since that's the way the function and its prototype are defined.

    If you want to get a floating point number between two given numbers, say fmin and fmax, here's a way

    First find a floating point number between 0 and 1. Then perform a scaling function that maps the interval [0,1] to [fmin,fmax].

    Here's the function (remember RAND_MAX is the largest possible value that can be returned by rand()).

    Code:
    double x;
    
    
    
    x = (double)rand()/RAND_MAX; /* this makes x between 0 and 1 */
    x = x * (fmax - fmin) + fmin;
    Of course you can do it in one step:


    Code:
    x = ((double)rand()/RAND_MAX ) * (fmax - fmin) + fmin;
    Here's a function built like the one you had, but the inputs and output are doubles:

    Code:
    double getfrand(double fmin, double fmax)
    {
       static int Init=0;
       double rc;
       if (Init==0)
       {
          srand(time(NULL));
          Init=1;
       }
       rc= ((double)rand()/RAND_MAX) * (fmax - fmin) + fmin;
       return (rc);
    }
    Regards,

    Dave
    Last edited by Dave Evans; 09-28-2004 at 08:56 PM.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    22
    thanks Dave!

    I figured out to use random integers and cast it double

    if I need -0.5 to 0.5

    int min -500 to 500
    then divide by 1000

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int getrand(int, int);
    
    int main (void)
    {
       int i, r;
       double random;
       printf("\nrandom numbers\n");
       for(i=0; i<20;i++)
       {
          r= getrand(-500, 500);
    	  random= (double)r/1000.0 ;
    	  printf("your number is %.3f\n", random);
       }
       return 0;
    }
    
    int getrand(min, max)
    {
       static int Init=0;
       int rc;
       if (Init==0)
       {
          srand(time(NULL));
          Init=1;
       }
    
       rc= rand()% (max-min+1)+min ;
       return (rc);;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL shooting game
    By sl4nted in forum Game Programming
    Replies: 6
    Last Post: 12-01-2006, 10:37 PM
  2. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 11:02 PM
  3. The wall behind is showing over the wall in front. (Glut)
    By Queatrix in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2005, 05:50 PM
  4. Question for Random class
    By joenching in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2005, 11:22 PM
  5. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM

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