Hey,
I am trying to learn the rand() function in C++
I got this program that prints 10 random nos but i dont get this program
Please clear the doubts i have for some lines which i have commented in the prog
#include<iostream.h>
#include<ctime.h>
void main()
{
using namespace std;
time_t t; // <<--------------I have no idea what this line does !!!
time(&t); // <<-------------- Same for this please help
srand(t);
for(Int iIndex=0;iIndex<10;++iIndex)
{
cout<<(rand()%4)<<" "; // <<---- Not sure how do the numbers appear as we have left a blank space in the commas ...
}
cout<<endl;
}
int main()
{
srand((unsigned)time(0));
int random_integer = rand();
cout << random_integer << endl;
}
1] Since the previous post mentions that the time function needs to be passed on a pointer only, why is and integer passed on to the function in the above program ?
2] What kind of parameter must be passed into the srand function ?
3] What does the unsigned keyword actually do ?
Actually I've got this project on a card game to be submitted in 45 days, so initially I am just trying to make the text version and I will advance later and thats why I'm doing this RnD on random ...
I also have this problem almost the same. I have a project which includes random selection.. a LOTTO 6/42. And I had a problem with the rand numbers appearing all the same.
e.g. Winning Numbers: 39 39 39 39 39 39
And I saw the correst usage of rand/time-t/&t above and I tried it and it works. I just have some sort of mistakes.
And thanks to hamsterman, I also have learned the 'unsigned'... Because I'm always getting a problem of warning: time_t is converted to unsigned int... Somewhat like that...
I think this site will give me more info. Thanks to all who replied here.