Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-11-2012, 02:08 AM   PM User | #1
sabi
New Coder

 
Join Date: Oct 2011
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
sabi is an unknown quantity at this point
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++;
}
}
sabi is offline   Reply With Quote
Old 10-11-2012, 08:17 AM   PM User | #2
nehadas87
New Coder

 
Join Date: Jun 2011
Location: India
Posts: 23
Thanks: 1
Thanked 1 Time in 1 Post
nehadas87 is an unknown quantity at this point
try this, num = 100 + rand() % 899;
__________________
God is Great !!
nehadas87 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:54 AM.


Advertisement
Log in to turn off these ads.