C++ help

What would i use in a c++ program for the program to give me random a set of numbers.?

I am writing a program that adds two random numbers and display the answer too an individual when the individual is ready. But dont know what i would need for the program to give the two numbers.


example 123
+ 123

are you tring to get a random number?

http://www.cplusplus.com/reference/cstdlib/rand/
U should use rand().
So would this mean it would give me number1 between 1-999


#include <iostream>
using namespace std;

int main()
{
int number1,
number2,
answer;

number1=rand() % 1000+999

}
if your trying to get between 1-999, use:
 
rand() % 999+1
So i got to work but it keeps giving me the same number every time i run the program.
do you mean same set of numbers, if so the just
generate new sets of number with this:
1
2
3
#include <time.h>

srand(time(NULL));
It gives you the same numbers because rand() gives you a set of pseudo-random numbers. You need change the seed that rand() uses. Use #include<ctime> and time(0) in your random number generator. You can search the time() function on this website for help on implementation. Good Luck! :)
1
2
#include <Windows.h>
srand (time(NULL));


srand = the generator for rand and it will use the time to generate a number. Since times never stop it will always generate a different number!!!!
Last edited on
Alright i got it. Thanks for the help!!
Topic archived. No new replies allowed.