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

Please answer this correctly
The length of rectangle B is 25% greater than the length of rectangle A. The width of rectangle B is 3/5 X the width of rectangle A. Find the fraction Area of
if n - 2y = 3y- n/m,find the value of n when y = 5 and m = -3( the answer is 7 1/2 but could you please explain )
Why is the nucleus important to a cell? 1 The nucleus organizes the DNA and allows for careful regulation of gene expression. 2 The nucleus regulates the cell’s
Starting with magnesium and oxygen atoms, use Lewis diagrams to show the formation of the ionic compound Mgo.
Need answer step by step
brief explanation on what the Apollo 11 mission was. dont get from the internet
How many drinks are in a 60 ounce pitcher of beer?
Prehistoric societies did not leave behind _____
Identify and explain the Oxymoron! - It was a minor miracle that no one was hurt when the car ran off the country road.