# [.net] [.NET] Random Float/Double

This topic is 4330 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need to generate random decimal numbers from X negative value, to Y positive value. System.Random only generates whole numbers this way, and only generates doubles from 0.0 to 1.0. What can I use to generate a random floating point value ranging from negatives to positives?

##### Share on other sites
There's a great thread about mapping a range of numbers to another range of numbers here.

The basics are thusly:

First, find the range of numbers that you need. In your case, that'll be (Y - X), yielding a positive number (if X is negative, it'll be bigger than Y). We'll call this range 'Z'. Multiplying the random number gotten from System.Random's random double by Z, the new random number will be from 0 - Z.

Next comes the shifting of the range from 0 - Z to be from X - Y. To do so, simply add X to the number. The negativity of X will work itself out.

The snazzy thing is that this method works for both positive and negative values for X and Y, assuming Y is the larger of the two (more positive, not greater absolute value).

Best of luck!
-jouley

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633661
• Total Posts
3013221
×