What is the difference between srand and rand
If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1. Apparently, on your system, seeds of zero and one have the same effect. I would guess that this is to provide backwards compatibility with some piece of software that expects srand 0 to reset the PRNG to its initial state.
If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.
Taken from the srand reference. When reading manual pages, they all state that "If no seed value is provided, the rand function is automatically seeded with a value of 1. That the same result happens for seeding with both 0 and 1 is most likely implementation dependant, and should not be counted on happening on all platforms. The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. Change Language. Related Articles.
Table of Contents. Save Article. Improve Article. Like Article. Previous Differentiate printable and control character in C? Next Program to reverse a string Iterative and Recursive. Recommended Articles. Queries to insert, delete one occurrence of a number and print the least and most frequent element. Article Contributed By :. Easy Normal Medium Hard Expert. One question here - How has the standard arrived at the large numbers inside the rand function?
What is the logic behind it? I agree with you on all points. But I think OP just wanted to know how rand knows what the seed is if srand doesn't return something you pass into rand as a parameter. Even if we don't shouldn't! Jimit Rupani Jimit Rupani 5 5 silver badges 14 14 bronze badges. Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta.
Now live: A fully responsive profile. Linked Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings.
0コメント