0

Can be talk me about Rand and srand in c++. Pls help.

26th Mar 2018, 11:59 AM
Ankesh Jain
Ankesh Jain - avatar
5 Respuestas
+ 1
In C++ as in most languages, rand is a formula to generate numbers attempting to make them random. The nice thing about it is that by default it always returns the same numbers so you can know what numbers are next and debug your code. Having truly random numbers means that a bug might only happen once a day and you never know when. srand is how you change where rand starts in the pattern. You pass a number in and the pattern generates the next number from there. To make a random starting number, we normally use the current time. If your program is fast and you manage it run it twice in a single second, both runs will be the same.
26th Mar 2018, 12:29 PM
John Wells
John Wells - avatar
+ 1
I don't understand this question. Do you have a program it is related to or a link to some place? If so, please post the link so I can put your question into context.
26th Mar 2018, 12:37 PM
John Wells
John Wells - avatar
+ 1
Since there wasn't a srand call made, you can run the program for the rest of your life and it always returns the same number.
26th Mar 2018, 12:45 PM
John Wells
John Wells - avatar
0
but , why let 41 ??
26th Mar 2018, 12:34 PM
Ankesh Jain
Ankesh Jain - avatar
26th Mar 2018, 12:43 PM
Ankesh Jain
Ankesh Jain - avatar