I had a stomach virus and missed class for a week and a half. I have emailed my teacher informing him and asking for the topics taught but he didn't message me back. I have no contacts in that class to help me out so I resort to this website. Please help me understand how to solve this homework problem!!
the last we did was the shift equivalent of arrays.
I dont understand how to use the rand() function and my efforts of researching it just confused me more all it does is produce a random number? how do i set the limits?
Please make the small effort of putting the homework problem quoted in your post, explaining exactly what is it you don't understand, and possibly showing the code that you wrote so far.
I dont understand how to use the rand() function and my efforts of researching it just confused me more all it does is produce a random number? how do i set the limits?
Before using rand() you must seed the random number generator.
You seed the RNG by using srand(). Call it just once, in main().
1 2 3 4 5 6 7 8
#include <cstddef>
#include <cstdlib>
int main()
{
std::srand(std::time(NULL));
// we seed the RNG with a "random" number: the current time
}
Now when you use rand() it will give you a big integer. You trim it down by using the modulo operation.
1 2 3 4 5 6 7
// ...
std::rand() % 10; // could be anything from 0 to 9
std::rand() % 3; // could be anything from 0 to 2
std::rand() % 210; // could be anything from 0 to 209
// ...
So now the final step is, how to also set a minimum value, so that it's not always 0?
In fact I'll just give you the formula, and you reason why it is correct (if it is).
std::rand() % (max + 1 - min) + min; // could be anything from min to max