...

View Full Version : random number generator, c++



Nightfire
03-30-2003, 06:09 PM
Can anyone see why this always shows the random number as 99?



#include <iostream>
#include <windows.h>

int main(){
int x = 1;
int y = 99;
std::cout << "A random number between " << x << " and " << y << " is: " << rand()%x+y;
return 0;
}

jkd
03-30-2003, 06:25 PM
You need to seed it:

srand(time(NULL))

Be sure to include time.h

willie
03-30-2003, 08:29 PM
Try rand()%(x+y). It will work better!

Nightfire
03-30-2003, 10:12 PM
Thanks :) Not only did the (x+y) work better, but it was the only way it would work :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum