Generating Random Numbers

This is a discussion on Generating Random Numbers within the C++ Programming forums, part of the General Programming Boards category; Can you tell me the code how to generate random numbers from 5-10 or tell me any links thanks....

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    69

    Generating Random Numbers

    Can you tell me the code how to generate random numbers from 5-10 or tell me any links thanks.

  2. #2
    Unregistered
    Guest
    Code:
    #include <stdlib.h> 
    #include <stdio.h> 
    #include <time.h> 
    
    inline int rand_mid(int low, int high)
    {
        return (rand() * (high - low)) / RAND_MAX + low;
    }
    
    int main(void)
    {
        int x;
        srand(time(NULL));
        
        x = rand_mid(5, 10);
        printf("A random number from 5 to 10: %d\n", x);
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    69

    Thanks

    Thanks just wondering can u redo it so it has comments just so i'm not mis-interpreting some parts of it.

  4. #4
    Unregistered
    Guest
    Code:
    #include <stdlib.h> //for rand, srand, and RAND_MAX 
    #include <stdio.h>  //for printf and you already know this ;p
    #include <time.h>   //for time to seed rand
    
    /*This function is declared as inline, meaning it is treated as a
    **macro instead of a function...this causes less overhead
    */
    inline int rand_mid(int low, int high)
    {
        /*take low out of high (subtract), then multiply the result
        **by a random number. Divide that result by the constant
        **RAND_MAX from stdlib and add low to that. The result is
        **a pseudo-random number within the bounds of low and high
        */
        return (rand() * (high - low)) / RAND_MAX + low;
    }
    
    int main(void)
    {
        int x;
        /*seed rand, this is done so that you don't get the same
        **set of random numbers every time you run the program
        **so you set the seed to the current time, which creates 
        **pretty good random numbers.
        */
        srand(time(NULL));
    
        /*pass 5 and 10 as low and high to rand_mid, then
        **assign the value returned to x and print it out
        */    
        x = rand_mid(5, 10);
        printf("A random number from 5 to 10: %d\n", x);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 01:29 PM
  4. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 02:16 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