What does Srand does? Explain with example | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What does Srand does? Explain with example

17th Oct 2016, 4:26 PM
buvanesh satish kumar
buvanesh satish kumar - avatar
3 Answers
+ 1
srand generates your random seed. typically you use srand (time (NULL)) Which will seed it to the current time of the cpu to generate a new unique number
17th Oct 2016, 5:11 PM
Daniel Rollins
Daniel Rollins - avatar
0
#include <cstdlib> #include <ctime> #include <iostream> using namespace std; int main() { srand(time(NULL)); int x = rand ()% 10 + 1;//generates a number from 0->9 then adds 1 return 0; }
17th Oct 2016, 5:43 PM
Daniel Rollins
Daniel Rollins - avatar
- 2
<?php //seed with micro second Function make_seed() {List($usec, $sec) =explode(' ' , microtime()); return $sec + $usec* 1000000; } Srand(make_seed()); $randval=rand(); ? >
17th Oct 2016, 5:38 PM
Yogesh Bhadoria
Yogesh Bhadoria - avatar