Ask Question
29 October, 14:59

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?

+5
Answers (1)
  1. 29 October, 18:47
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers