Thread: Random number in range generation.

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    84

    Random number in range generation.

    Hi,
    I've seen various methods for generating a random number within a range around the internet and in books, but some are very roundabout and I'm wondering if someone could provide me with the simplest method for generating a random number in a certain range. My program will have to do this many times, and so I would like as much succinctness as possible.

    An instance of what would need to be calculated:

    /* I would like floating point random numbers with three decimal places.
    The range should be set by another place in the program, and so I need variables as the values of RangeMin & RangeMax */

    float RangeMin;
    float RangeMax;

    Rand_Num1 = rand(....in the range of Min to Max...)
    etc


    Thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here's a good article on the ins and outs of using rand().

    http://www.eternallyconfuzzled.com/a..._art_rand.aspx

    gg

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    As you can tell by reading the excellent article on the linked site, random number generation is not about being succinct, it's about the *quality* of the random numbers, that you need for your program.

    For a simple program, where no serious consequences are afoot, the simple ways will do fine. Just be aware that they are simple, and rudely different than true random numbers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Is there something wrong with this implementation?
    It compiles, but when it runs, the program ends without displaying anything.

    THANKS!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    float Tval_v_0_maxZ;
    
    
    float Tval_force_v_0;
    
    float f_v_0_x;
    float f_v_0_y;
    float f_v_0_z;
    
    float r_v_0_x;
    float r_v_0_y;
    float r_v_0_z;
    
    
    
    // Xforce = Tval(Xreal);
    // Yforce = Tval(Yreal);
    // Zforce = Tval(Zreal);
    
    
    int main (void) {
    
    r_v_0_x = 10;
    r_v_0_y = 15;
    r_v_0_z = -40;	
    	
    
    /* case p */
    
    	Tval_v_0_maxZ = -20 / r_v_0_z;
    
    /* Vertex 0 */
    do
    	Tval_force_v_0 = rand();
    while (Tval_force_v_0 > Tval_v_0_maxZ);
    
    f_v_0_x = Tval_force_v_0 * r_v_0_x;
    f_v_0_y = Tval_force_v_0 * r_v_0_y;
    f_v_0_z = Tval_force_v_0 * r_v_0_z;
    
    printf("Force Vertex 0 is (&#37;f,%f,%f) using Tval_force_v_0 of %f \n",f_v_0_x,f_v_0_y,f_v_0_z,Tval_force_v_0);
    return(0);
    }

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    It is very strange. It simply will not run, but it compiles. This happened with another script of mine, which I know was working the previous night.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    By process of elimination, I found that only my rand function was causing problems.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Could anyone please break down the necessary parts of a program that includes a random number generator? What I mean is, I'm finding it hard to decipher what goes before "int main (void) {"
    and such. I want to generate many random floating point numbers in my program (with greater-than/less-than ranges). These random numbers will be used in the main body of the program and the ranges generated for them will be provided by user inputs.

    Thanks. Randomness is tough.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Quote Originally Posted by Codeplug View Post
    Here's a good article on the ins and outs of using rand().

    http://www.eternallyconfuzzled.com/a..._art_rand.aspx

    gg
    Let us know if there's anything in there you don't understand.

    gg

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    I guess I just don't understand the parameters for providing a range in:

    [CODE]
    unsigned time_seed()
    {
    time_t now = time ( 0 );
    unsigned char *p = (unsigned char *)&now;
    unsigned seed = 0;
    size_t i;

    for ( i = 0; i < sizeof now; i++ )
    seed = seed * ( UCHAR_MAX + 2U ) + p[i];

    return seed;
    }

    srand ( time_seed() );
    [/CODE}

    Some of the other examples on that site have 'M' and 'N' value, but I'm unclear about how to do that in the above version.
    Particularly if I want floating integers for a range of negative numbers. (though I do understand how to put floating integers into the earlier examples.)

    What do you put instead of:
    Code:
    int r = M + uniform_deviate ( rand() ) * ( N - M );
    from the earlier example?

    Thanks.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Some of the other examples on that site have 'M' and 'N' value, but I'm unclear about how to do that in the above version.
    You want a random float in a range using the code from that link? I'm not sure what all that stuff is for, but this works and uses code I pinched from the page. Don't ask me how it works though.
    Code:
    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    double uniform_deviate ( int seed )
    {
      return seed * ( 1.0 / ( RAND_MAX + 1.0 ) );
    }
    
    unsigned time_seed()
    {
      time_t now = time ( 0 );
      unsigned char *p = (unsigned char *)&now;
      unsigned seed = 0;
      size_t i;
    
      for ( i = 0; i < sizeof now; i++ )
        seed = seed * ( UCHAR_MAX + 2U ) + p[i];
    
      return seed;
    }
    
    int main(void)
    {
        double m, n;
        int i;
    
        srand(time_seed());
    
        printf("Please enter a range: ");
        scanf("%lf%lf", &m, &n);
    
        for ( i = 0; i < 25; ++i )
        {
            double r = m + uniform_deviate(rand()) * (n - m);
    
            printf("[%g..%g): %f\n", m, n, r);
        }
    
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Hi, thanks.
    That works. But I'm noticing some very recognizable trends in the outputs: in the generation of only 11 numbers, I had 2 that were the same.
    That website hyped this as a great solution, but I'm not totally seeing it.

    At the very least, I can't have it creating the same four decimal point float in a range of 400.
    Are there any other methods that might aid this?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Random numbers are likely to repeat now and again. How often depends on the implementation of the random number generator.

    This version uses whatever implementation is in the C runtime library - which may not be very good in your particular situation.

    I copied the above code, and in the 25 numbers listed, I couldn't see a duplicate for the range [0..400) that you get for inputting 0 400 to the app.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What M and N are you using?

    gg

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by hebali View Post
    At the very least, I can't have it creating the same four decimal point float in a range of 400.
    Are there any other methods that might aid this?
    If that's really an issue, then you don't want random numbers -- or more to the point, you need a random shuffle. My back of the envelope calculation says that for what you gave (4000000 possible numbers), in 11 trials you'll get a repeated number 1 out of 50000 times.

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    My M and N are negative number:
    M = -10, N = -400
    how does this affect the equation?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number generation
    By parisha in forum C Programming
    Replies: 9
    Last Post: 12-17-2008, 11:00 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. Random number generation
    By Lisa Mowbray in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 12:22 PM
  5. Random number generation
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-16-2002, 06:45 PM