#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
int a,b;
for(;;){
cout << "please give me your max number: ";
cin >> a;
srand(time(0));
b = (rand() % a) +1;
cout << b << endl;
system("pause");
cout << endl;
}
}
ok people this is a program that i found and i saved it because i wanted to remember the srand and rand and the other staff(stuff ain't sure :D). Anw the thing is that i want to ask what each line does
eg:
- ctime is the library that you can use to "manipulate" the time of your program
now i want to know about these specific lines:
srand(time(0));
b = (rand() % a) +1;
for example, why the "%" symble is here (i know what it does in math, i think it's the mode) but anw that's it
srand is a function that seeds the random number generator. Random number generators are (usually) not random. They start with one number and use that to make the next one, and so on. Using the time as a seed gives you a rough approximation of a random starting number.
% is the modulus operator. It's here because the programmer wanted to restrict the range of random numbers.