rand()

Please Check And Solve The Problem it Shows Only 41

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<stdlib.h>
using namespace std;

main()
{

int i;
i = rand();
cout<<i;

}

Last edited on
rand() generates a fresh value each time it is called. In your case, the first value in that sequence is apparently 41.

However, the values are derived by doing some calculation on a previously stored value. To get a different sequence of values, you need to change the starting condition by supplying a seed value. Usually the current time is used as it will tend to differ each time the program is run.

Something like
 
    srand (time(NULL));


Note, you should call srand() just once at the start of main().


See
http://www.cplusplus.com/reference/cstdlib/srand/
http://www.cplusplus.com/reference/ctime/time/

Topic archived. No new replies allowed.