Ask Question
3 December, 10:21

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?

+3
Answers (1)
  1. 3 December, 10:35
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers