CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Computer Programming (http://www.codingforums.com/forumdisplay.php?f=21)
-   -   Need help with generating random numbers (http://www.codingforums.com/showthread.php?t=276077)

sabi 10-11-2012 02:08 AM

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++;
}
}

nehadas87 10-11-2012 08:17 AM

try this, num = 100 + rand() % 899;


All times are GMT +1. The time now is 08:14 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.