Search This Blog

Monday, 5 November 2012

Difference between Rand & Srand


You can use rand without srand (but the problem will be that you'll get same output whenever you run your program). Srand is a "seed value". As you sow so shall you reap. A group of random numbers are generated with each particular value of srand. Whenever we use a specific value in srand, a specific group of numbers will be generated, and that specific group of values (so called random) can be obtained whenever we use that number in srand which we used previously.


Note: C Standard Library (cstdlib) has been included for using srand and rand statements.





















Q. What does it really mean if we use srand (5); in a program?

Ans: It means that a set of predefined operations will be carried out at 5, and then a number will be generated (using rand).
User can use this number. If called second time (using rand) again set of predefined operations will be performed to produce second number. These Operations are set in such a way that they produce random number.

No comments:

Post a Comment