Ask Question
19 March, 00:39

in c++, what happends when i add 1 to the RAND_MAX then take rand () deivded by (RAND_MAX+1) ? if my purpose is to take out a random number within 0=< and = <1?

+2
Answers (1)
  1. 19 March, 01:14
    0
    rand () function is used to generate random integers between the range 0 to RAND_MAX. So if we divide the the rand () function by RAND_MAX the value returned will be 0.

    You cannot do RAND_MAX + 1 the compiler will give error integer overflown.

    To generate an integer with a range. You have to do like this:

    srand (time (0));

    int a=-1 * (rand () % (0 - 5245621) + 1);

    cout <
    This piece of code will return an integer within the range 0 to - 5245622.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “in c++, what happends when i add 1 to the RAND_MAX then take rand () deivded by (RAND_MAX+1) ? if my purpose is to take out a random number ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers