## newbiecoder

Hello I'm trying to get a random float number between two float numbers using rand() function but I couldn't write it by myself, can you show me how to do it? Thanks...

## UncleLeroy 49

Try something like this:

``x = low_number + (high_number - low_number) * (rand() / RAND_MAX) / 2.0;``

If you need more help, follow the policy of this forum, by writing some code yourself, and do not continue to ask for help without showing any effort on your part.

## glutisnotasin

I have a similar problem. However this doesn't seem like a solution, because it only redistributes the possible random outputs so that they are spaced with regular intervals between low_number and high_number. There are still only RAND_MAX+1 different possibilities. What if I want the output to be ANY float?

Is there a function that does this? I've been trying to find for one several days without luck.

## firstPerson 761

>>What if I want the output to be ANY float

rand() has a limit on the highest number it can return, specifically RAND_MAX. So if
you want to output any float number you need to adjust accordingly( or use a better RNG).

## arkoenig 340

The problem is solvable in principle, but it isn't easy. Here's why.

Suppose you're trying to obtain a random number between 0.0 and 1.0, and you want the result to be uniformly distributed. That does not mean you want every possible floating-point number to appear equally likely, because the little tiny numbers close to 0, with large negative exponents, are much more closely spaced together than the bigger numbers near 1.

So if you want uniform distribution, and you also want every floating-point number to be possible as a result, you wind up with a whole bunch of tiny numbers that, because they're so close together, occur extremely rarely--but still have to be able to occur at all.

Figuring out how to do this reliably is far from easy, especially if your integer random-number generator isn't very good (as many of them aren't).

So before diving into this problem, you might want to think a bit about whether it's really what you want.