5

EDIT I will re-explain, whenever I say decimal, I mean system.decimal not the mathematical concept

decimal min = 5.62;
decimal max = 14.39;

How would I get a system.decimal that is randomly between the range of the above two decimals?

Double != system.decimal

FYI I don't know how I can make my question clearer since more than half the people that read this only read 2 words and then flagged it as a duplicate.

ono2012
  • 4,967
  • 2
  • 33
  • 42
BananaPoop
  • 123
  • 1
  • 1
  • 8
  • Despite what you might think. The question this is a duplicate of answer is what you are looking for. **A decimal between two Decimals makes NO SENSE** The reason it makes no sense is because 5.5 is a valid decimal value. – Security Hound Jan 26 '12 at 17:21
  • @Ramhound I am not looking for simply a random decimal, i don't know how clear i have to make this, The system.decimal has to be random, BUT bewteen a range of two system.decimal 's Thank you – BananaPoop Jan 27 '12 at 07:25

4 Answers4

4

Scale your decimals to ints or big ints (by multiplying by an appropriate power of 10. Generate an int between them, then divide by a power of 10.

Or generate an int and then just scale it (linearly) between the decimals.

Cade Roux
  • 88,164
  • 40
  • 182
  • 265
  • If you want a double you need to generate a double, not an int, unless you only want a short, fixed precision. – Servy Jan 26 '12 at 16:29
  • @Servy The C# decimal the OP refers to is not a double, it's a fixed point number and can be scaled: http://msdn.microsoft.com/en-us/library/364x0z75%28v=vs.80%29.aspx – Cade Roux Jan 26 '12 at 16:36
4
public static double randomDouble(Random rand, double start, double end)
{    
    return (rand.NextDouble() * Math.Abs(end-start)) + start;
}
Servy
  • 202,030
  • 26
  • 332
  • 449
  • 3
    OP clearly asking about decimal, not double. – spender Jan 26 '12 at 16:33
  • @BananaPoop http://msdn.microsoft.com/en-us/library/system.random.nextdouble.aspx – Servy Jan 26 '12 at 17:09
  • @spender at the time I wrote the post the OP's use of 'decimal' was ambigous between the data type and the mathematical concept (of which a double most certainly applies). His specific statement, along with his comment here, state that he didn't even know that Random could generate a random double, the thought it could only generate Ints. – Servy Jan 26 '12 at 17:10
  • @BananaPoop if it really is imperative that you use decimal and not double, then just add my code to the link listed as the duplicate and change all of the data types in my method accordingly. – Servy Jan 26 '12 at 17:16
  • @BananaPoop - This is a duplicate despite the fact you think its different. You have not given a valid reason or pointed out HOW its different. A decmial value between two integers added to the lower integer does exactly what you want. – Security Hound Jan 26 '12 at 17:23
  • 1
    He wanted to generate a random Decimal, Just that, I need to generate a Random Decimal BUT it also has to be bewteen to other Decimals, random.next() only accepts ints, so i could get a decimal, but it would be bewteen to ints – BananaPoop Jan 27 '12 at 07:11
4

y=mx+c. Generate a 0<=X<1 FP random with NextDouble(), multiply it up by (Dmax-Dmin) into the right range, then add Dmin to shift the base.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • As there are only so many values double can represent between 0 and 1 your algorithm does never select a whole bunch (most) possible decimals. – D.R. Sep 24 '18 at 13:15
3

Here is a resource right here on StackOverflow ;)

Generating a Random Decimal in C#

or you could try:

Random random = new Random();
double mathResult =  Math.Round((random.NextDouble() / 100), 3);

Hope this helps!

Eugenio Miró
  • 2,398
  • 2
  • 28
  • 38
dmarges
  • 361
  • 2
  • 7