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

The molecules responsible for membrane transport are
True or false? data warehousing is the process of extracting data from a data warehouse.
Find 6 irrational no. Between 3 and 4
Once defining stress as an inability to cope, stress expert joan borysenko now defines stress as
Is LiOH soluble or insoluble? Is Na2CO3 soluble or insoluble? Cu(OH)2 soluble or insoluble?
solve 1 over 36 = 6^x-3
In humans, the number of tetrads formed during mitosis is ______.
What is the equation of the line perpendicular to y=3x+2 that passes through the point (0, 2)?
Which propaganda technique is used in this sentence? The world is moving to 3-D television. When will you? a. Snob appealB. Glittering generalitiesC. BandwagonD
Which of the following is a characteristic of an editorial? writing that is objective and unbiased writing that uses dialogue as its primary format writing to c