tags:

views:

769

answers:

8

So maybe I want to have values between -1.0 and 1.0 as floats. It's clumsy having to write this and bolt it on top using extension methods IMO.

+16  A: 

There's a .NextDouble() method as well that does almost exactly what you want- just 0 to 1.0 instead of -1.0 to 1.0.

Joel Coehoorn
D'oh. Thats a way better answer +1.
AnthonyWJones
Thanks, I remembered that now, but it still doesn't support from to, but from 0 to n, which is stil lacking. Why not also have an overload with a to b, right?
Joan Venge
It's not that hard- still just two calls: one to Random.Next() for the integer portion and another to NextDouble() for the mantissa.
Joel Coehoorn
2 call + negative inversion?
Joan Venge
Is there some reason the more canonical (NextDouble() * 2 - 1) doesn't work? Are all the bits of the mantissa significant in your application?
Greg D
I was thinking more for any range, not just -1 to 1, since over a large range a single call wouldn't have the same precision.
Joel Coehoorn
Yes, I meant from n to m, where n can be negative, etc. Also it's in Random(int) but not Random(double) makes me wonder.
Joan Venge
(NextDouble() * (max - min) + min) will work if "mostly random" is random enough.
Greg D
+1  A: 

The vast majority of random number usage I've seen (in fact all I've ever seen, although apparently in 3 decades of programming I haven't seen much) is generating random integers in a specified range.

Hence Random.Next seems very convenient to me.

AnthonyWJones
Then you haven't seen much. :) In CG, random floats are very common.
Joan Venge
Anthony, it depends on the type of programming you do. Just because someone programs say for instance databases doesn't mean he has seen all there is to programming. Hence I mentioned CG.
Joan Venge
I wonder how many database style applications there are in the world in total compared to the number of CG apps?
AnthonyWJones
+4  A: 

Because most of the time, we want an int within a range, so we were provided with methods to do that. Many languages only support a random method that returns a double between 0.0 and 1.0. C# provides this functionality in the .NextDouble() method.

This is decent enough design, as it makes the common easy (Rolling a die - myRandom.Next(1,7);) and everthing else possible. - myRandom.NextDouble() * 2.0 - 1.0;

chris
If you are working with floats then, having a random float generator from -n to n is more useful.
Joan Venge
Then write a one line extension method and use it to your hearts content
frou
Rolling a die is apparently not easy enough - it's myRandom.Next(1, 7) as the upper limit is exclusive :)
Jon Skeet
When Jon Skeet calls myRandom.Next(1, 21), it always returns 20...
Bearddo
A: 

It's clumsy having to write this and bolt it on top using extension methods

Not really - extension methods are succinct and I'm glad the feature is there for things like this.

frou
Extension methods are good but you are stuck if the method was a static method as static or a property (static or instance).
Joan Venge
+3  A: 

You can use the Random.NextDouble() method which will produces a random value between 0.0 and 1.0 as @Joel_Coehorn's suggested

I just want to add that, extending the range to -1.0 and 1.0 is a simple calculation

var r = new Random();

var randomValue = r.NextDouble() * 2.0 - 1.0

Or to generalize it, to extend NextDouble() result to any range (a, b), you can do this:

var randomValue = r.NextDouble() * (b - a) - a
chakrit
You wanted to say "..the range to -1.0 and 1.0 is a ..."
Germstorm
+1  A: 

On a side note, I'd have to point out that, at last, the random-number generation algorithm in a framework is not something clumsy (like cstdlib's rand()) but actually a good implementation (Park&Miller, IIRC).

Dmitri Nesteruk
A: 

This method isn't that hard to add, seems like the perfect usage of an extension method.

public double NextSignedDouble(this Random r)
{
    return (r.Next(0, 2) == 0) ? r.NextDouble() : (r.NextDouble() * -1);
}
chills42
+4  A: 
public static double NextDouble(this Random rnd, double min, double max){
    return rnd.NextDouble() * (max - min) + min;
}

Call with:

double x = rnd.NextDouble(-1, 1);

to get a value in the range -1 <= x < 1

P Daddy
Thanks, I guess I will go with this.
Joan Venge