Need to understand this random number program



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

I got the program from this link
http://xoax.net/comp/cpp/console/Lesson22.php
1
2
time_t t; // <<-------Declare a variable t of type time_t 
time(&t); // <<------Use the time() function to initialise t to the current time 

See http://www.cplusplus.com/reference/clibrary/ctime/ for more info if interested.

 
cout<<(rand()%4)<<" "; // <<---Output random number followed by a space 


See http://www.cplusplus.com/doc/tutorial/basic_io.html
1] What do you mean when you say Declare a variable t of type time_t ??
Does time_t become something like a new add on data type ??

2] Why do we pass &t in time() and why not just t

3] What does the %4 sign indicate after rand() ??
1. time_t is a data type (just like int or char)

2. becouse function "time" requires a pointer.
see http://www.cplusplus.com/doc/tutorial/pointers.html

3. % is a modulo operator. it gives the reminder of division
x 0 1 2 3 4 5 6 7 8 9...
x%4 0 1 2 3 0 1 2 3 0 1...
% is a modulo operator. it gives the reminder of division

Let's make that the "remainder" of the division.
Ok thanks for all those replies.
You guys have really helped a lot to understand the thing
Now I came across this program :

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

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 ...
1.in this case 0 is a NULL pointer.

2.unsigned int.

3."unsigned" makes variable have only positive values:
int is from -2147483648 to 2147483647
unsigned int is from 0 to 4294967295
see http://www.cplusplus.com/doc/tutorial/variables.html
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.
Topic archived. No new replies allowed.