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

Write the formulae of the acid and base of ZnS .​
There are 20 people in a room. 4 of them are people you know. When 2 people are selected at random at the same time, what is the probability that at least one o
Someone pls help me. i dont understand this at all :c
A person who argues for a cause; a supporter or defender:
I need help filling in the blank space.
How many moles of Cl atoms are in a 75.0 ml. sample of CCM (d:1.6 g/cm?, C. 12, CE36)
Which idea from the passage is also communicated by the advertisement on page 6? the bicycle helped establish women’s rights to join organizations formerly una
Can someone help please! , thank u.
Which number line represents 3/2?
I need help with this please help me