What's the difference between rand() and srand()? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's the difference between rand() and srand()?

I want to know diff between random and truly random no

21st May 2017, 10:36 AM
Sukumar Patnaik
Sukumar Patnaik - avatar
12 Answers
+ 5
we usually give it the time. that way srand always has a unique value
21st May 2017, 11:01 AM
jay
jay - avatar
+ 4
watch video on how to do it and follow along in codeplayground. it will make it alot easier to understand if you see it in action
21st May 2017, 11:02 AM
jay
jay - avatar
+ 4
oooooo i understand now.. if we use the same seed rand will give the same results
21st May 2017, 11:07 AM
jay
jay - avatar
+ 4
computers can not give true random numbers on their own if that is what you are asking
21st May 2017, 11:08 AM
jay
jay - avatar
+ 3
they are used together. srand is used to give rand a seed value. Have you ever played minecraft? If so do you know about world seeds? and how if you enter one the world that is generated is always the same whenever you reenter that seed? that is what srand is used for. we generate random numbers by giving srand a unique seed value. Usually by using the current time. so when we use rand the numbers it produces are always unique (as it is never the same date and time) so for example srand(9); \\ when using 9 as seed rand will always produce the same set of random numbers srand(time(0)); \\ when using time as a seed rand will produce a different set or random numbers hope this helps
21st May 2017, 10:44 AM
jay
jay - avatar
+ 3
srand is used with rand. without it rand will always give the same numbers. they will be random but they will be the same everytime the program is run. by using srand with rand we give the computer something to use to help randomise with. you will never get truly random numbers from rand. but they are good enough for many purposes.
21st May 2017, 10:56 AM
jay
jay - avatar
21st May 2017, 10:59 AM
jay
jay - avatar
0
pls explain seed and truly random no meaning
21st May 2017, 10:48 AM
Sukumar Patnaik
Sukumar Patnaik - avatar
0
does that mean we give a random no from our side!
21st May 2017, 10:59 AM
Sukumar Patnaik
Sukumar Patnaik - avatar
0
no I wanted to ask whether the seed value creates a truly random value or it just pretends to be by taking our input
21st May 2017, 11:02 AM
Sukumar Patnaik
Sukumar Patnaik - avatar
0
thanks buddy
21st May 2017, 11:07 AM
Sukumar Patnaik
Sukumar Patnaik - avatar
0
then srand() is just like following a bit diff path to reach same result!!!
21st May 2017, 11:09 AM
Sukumar Patnaik
Sukumar Patnaik - avatar