Thread: Random numbers are not in specified range

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    13

    Question Random numbers are not in specified range

    My program behaves weird...

    I wanted to generate 10 random numbers from 1 to 100 each of them bigger than previous, using the while loop and function that returns a random number in specified range.

    When I run the program, I get numbers much bigger than 100, even negative number, and numbers are same every time I run the program.

    Can someone tell me explanation or alternative please?

    Here is the code:

    Code:
    #include <ctime>#include <cstdlib>
    #include <iostream>
    
    
    using namespace std;
    
    
    int range(int low, int high);
    
    
    
    
    int main()
    {
        srand(time(NULL));
    
    
        int a1;
        int a2;
        int a3;
        int a4;
        int a5;
        int a6;
        int a7;
        int a8;
        int a9;
        int a10;
    
    
    
    
        while(a1 < a2 && a2 < a3 && a3 < a4 && a4 < a5 && a5 < a6 && a6 < a7 && a7 < a8 && a8 < a9 && a9 < a10 )
        {
            a1 = range(1, 100);
            a2 = range(1, 100);
            a3 = range(1, 100);
            a4 = range(1, 100);
            a5 = range(1, 100);
            a6 = range(1, 100);
            a7 = range(1, 100);
            a8 = range(1, 100);
            a9 = range(1, 100);
            a10 = range(1, 100);
        }
    
    
        cout << a1 << endl << a2 << endl << a3 << endl << a4 << endl << a5 << endl << a6 << endl << a7 << endl << a8 << endl << a9
             << endl << a10;
    }
    int range (int low, int high)
    {
        return rand() % (high - low) + low;
    }
    Here is the output:

    4286446
    2686872
    4286352
    1976199838
    1976195178
    -2
    1058225792
    1976283853
    2686924
    2686728
    Process returned 0 (0x0) execution time : 0.016 s
    Press any key to continue.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Are you sure your while statement is even executing? I doubt very much that it is since you're using all those uninitialized variables.

    Have you heard of vectors or arrays?


    Jim

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Well, thanks!

    That was it!


    Quote Originally Posted by jimblumberg View Post
    Have you heard of vectors or arrays?
    I am just up to that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. drand48(): generating pseudo random numbers in a range
    By Isolda_ in forum C Programming
    Replies: 2
    Last Post: 08-31-2007, 11:21 AM
  2. random number in range
    By lgg in forum Linux Programming
    Replies: 3
    Last Post: 08-14-2005, 05:15 AM
  3. Random Numbers within a range OTHER than 1-X
    By Kaelin in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2005, 11:57 AM
  4. Replies: 10
    Last Post: 11-23-2001, 10:01 AM
  5. generating random numbers within a range
    By tucky in forum C Programming
    Replies: 3
    Last Post: 09-14-2001, 12:59 PM

Tags for this Thread