Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need help with generating random numbers

    I need to generate random numbers between 100 and 999
    this is what i got but this doesn't work every time. It goes over 999 sometimes


    #include <iostream>
    #include <cstdlib>
    #include <ctime>

    using namespace std;

    int main()
    {
    int num;
    int count = 1;
    unsigned seed = time(0);

    srand(seed);

    while(count <= 20)
    {
    num = 100 + rand() % 999;
    cout << num << endl;
    count++;
    }
    }

  • #2
    New Coder
    Join Date
    Jun 2011
    Location
    India
    Posts
    23
    Thanks
    1
    Thanked 1 Time in 1 Post
    try this, num = 100 + rand() % 899;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •