Answer the question
In order to leave comments, you need to log in
How to calculate the rand() function?
Initially, I set the boundaries of the segment. Then it must be written to a function.
For example, for the interval [-5., 5.] the function will look like this:
double random()
{
return (double) (-5.0) + (double)rand() / RAND_MAX * 10.0 ; // a = -5; b = 10
}
Answer the question
In order to leave comments, you need to log in
inline int getRandomNumber(int start, int end) {
return start + rand() % (end - start);
}
double random(int a, int b)
{
return (double) a + (double)rand() / RAND_MAX * (b-a) ;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question