#include <stdlib>?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio>
#include <stdlib>
int main()
{
	int a,b,c,d;
   cout<<"Enter the starting number"<<endl;
   cin>>a;
   srand(a);
   b=rand()%RAND_MAX;
   c=rand()%200;
   d=rand()%10+200;
   cout<<"The number between 0 and "<<RAND_MAX<<" is : "<<b<<endl;
   cout<<"The number between 0 and 199 is : "<<c<<endl;
   cout<<"The number between 200 and 210 is : "<<d<<endl;

   getch();
   return 0;
}


1: can anyone help to explain what the above code do? I got it from the book...But not really understand it...
2. #include <stdlib> <-what is this library use for? Is that use for random?

TQ...
Do yourself a favor and toss that book. I say this because if this is a direct copy of the text then the author has no idea what a namespace is or a stream buffer for that matter.

As for your questions, stdlib is a header file not an actual library (there is a difference). Yes, it contains the functions srand() and rand() for psuedo random number generation here is a break down of what that header contains: http://www.cplusplus.com/reference/clibrary/cstdlib/
Last edited on
Thanks for the website...after read the example here..I still don't understand the meaning of it, can anyone help? TQ

( value % 100 ) is in the range 0 to 99
( value % 100 + 1 ) is in the range 1 to 100
( value % 30 + 1985 ) is in the range 1985 to 2014
You're looking at rand(), yes?

When using the rand() function, the number after the % is the number of random numbers generated. The value after the + tell it where to start from.

value % 10 + 30, for example, will generate a random number from the range 30-40.
Thanks iHutch

value % 30 + 1985
after +, the value is bigger than before +, is that means start=1985, end=1985+30=2015(need to add with value before +)? and somemore, from the website it gives only 2014?

value % 100 + 1
after +, the value is smaller than before +, is that means start=1, end=100(no need add with the value before +)??


TQ>..
Topic archived. No new replies allowed.