When I instruct my program to select a random value from an array, I do this array[rand() % X]and don't have to specify srand(time(0))
whereas In other cases I do have to specify it. Why so?
If you do not call srand() your "random" sequence will always be the same.
If you want the same random numbers every time you run your program, no need to call srand().
If you want different numbers each run, call srand() ONCE, at the beginning of your program.
Recommended: ALWAYS seed the random number generator.
If you write C++ code you should stop using the C library when C++ provides the same functionality. Use what the <random> and <chrono> headers give you to use.
rand() generates integer random numbers only. Using <random> you can generate floating pointer numbers as well as integers.
There are multiple ways to generate a sequence of random numbers with <random>. You can have uniform number distribution, bell curve type distribution, etc.
Even the C standard recommends not using srand()/rand() if there are other methods available.
you can use this to your advantage.
if you call srand(17) and then get 10 random values, and then call srand(17) and get 10 more values, the first 10 and second 10 will be the same. this is useful for testing (to run the same test set every time) and it is useful for a few algorithms (consider, a weak encryption, get srand's seed from user as their encryption password/key and then xor data with the bytes from rand, then repeat this to decrypt. Its not exactly the best thing going but its fast and sometimes you just want to discourage intruders (example, keep your peers from stealing your homework). You can also use it as a way to do a hash-table .. srand(yourdata) rand()%arraysize is now your hash function. got a collision, just run it until you don't. not only is it very lazy, but it does a solid job (again, using random instead is the way to go here).
<random> can to this as well, so this isnt any reason to use C's tools