icdvjnf9700 icdvjnf9700
  • 14-09-2019
  • Computers and Technology
contestada

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?

Respuesta :

ExieFansler ExieFansler
  • 20-09-2019

Explanation:

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 <<a;

This piece of code will return an integer within the range 0 to -5245622.

Answer Link

Otras preguntas

What is the author’s purpose in writing the second paragraph of “The Sandia Mountains”? to give the reader basic statistics about the Sandia Mountains to per
Who arrived in Newport, Rhode Island, helping Washington to turn the tide of the war?
Joe borrowed $7000 from the bank at a rate of 7% simple interest per year how much interest did he pay in six years
Can somebody help me solve this?
How do electrons in the same orbital move?
Worth a lot of points will give Brainly aswell! :) Week One (Chapters 1-6) Vocabulary 1. Record 3-5 words you did not know or you think are important to this
how many times can 87 go into 392​
What is the value of f(-1)? O f(-1) = -3 Of(-1) = -1 Of(-1) = 0 O f(-1) = 6
According to Lewis, what was Dickinson “adept” at doing?
Texas was no longer under Spanish Rule because?