D
D
Denis Akhunov2020-10-22 17:21:39
C++ / C#
Denis Akhunov, 2020-10-22 17:21:39

How to make real numbers using rand()?

How to make real numbers using rand()?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
LoliDeveloper, 2020-10-22
@denywho

Divide by a constant

double fRand(double fMin, double fMax)
{
    double f = (double)rand() / RAND_MAX;
    return fMin + f * (fMax - fMin);
}

A
Alexander Ananiev, 2020-10-22
@SaNNy32

https://stackoverflow.com/questions/2704521/genera...

J
jcmvbkbc, 2020-10-22
@jcmvbkbc

There are many ways to do this, the answer depends on what distribution you want and how many significant bits you need.
In the simplest case, if the distribution is uniform (like rand), and the number of bits is not important, then divide by RAND_MAX and bring the result to the desired range.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question