Sign in to follow this  
brwarner

Random Numbers in C#

Recommended Posts

brwarner    100
I need to generate random floats in C#, both positive and negitive. There seems to exist no built in function. I tried the Random class but the only float generator only gives numbers from 0 to 1 and there are no negitives. Can someone tell me a function or explain how to write one??

Share this post


Link to post
Share on other sites
BTownTKD    205
I don't use C#, but from what you've said, you can create a random function using the already-available random float value by multiplying by some factor of 10.

For negative values, generate a second random float, multiply it by ten, and if it's even, make the original number negative

Share this post


Link to post
Share on other sites
RenderTarget    398
Quote:
Original post by brwarner
I need to generate random floats in C#, both positive and negitive. There seems to exist no built in function. I tried the Random class but the only float generator only gives numbers from 0 to 1 and there are no negitives. Can someone tell me a function or explain how to write one??


Just scale the number it gives you to the range you want. For instance, if you want a range of -10.0f to 10.0f:

Random r = new Random( seed );

fVal = (float) r.NextDouble() * 20.0f - 10.0f;

You lose a teeny bit of precision, but usually not enough to matter.

Share this post


Link to post
Share on other sites
Premandrake    175

float RandomToScaled(double r, double maximum)
{
return (r - 0.5) * 2 * maximum;
}

Random random = new Random();

// This will return a number between -5000 and 5000.
RandomToScaled(random.NextDouble(), 5000);

Share this post


Link to post
Share on other sites
RenderTarget    398
Quote:
Original post by Drew_Benton
This page has a great example of what you want to do, I think. It worked for ints but I did not try it for floats, although I think that is covered.


It doesn't cover floats.

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Quote:
Original post by RenderTarget
It doesn't cover floats.


Sorry, maybe I should have posted what I was thinking:


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static Random random = new Random();

private static double RandomDouble(int min, int max)
{
double result1 = random.Next(min, max);
double result2 = random.Next(min, max);
while(result2 == 0)
result2 = random.Next(min, max);
return (double)(result1 / result2);
}

static void Main(string[] args)
{
for (int x = 0; x < 100; x++)
{
double var1 = RandomDouble(-10, 10);
System.Console.WriteLine(var1);
}
}
}
}




The whole "while(result2 == 0)" is to make sure we don't divide by 0. This is one way I thought of from reading that article.

After looking at the output:

-2.25
-5
-0.888888888888889
0.428571428571429
2.5
-4
-0.875
5
2.33333333333333
0.9
1.4
0
1.66666666666667
-0.125
1
0.222222222222222
-0.5
0.333333333333333
1.33333333333333
-0.125
-2.33333333333333
-1.8
1.8
0
-1.125
-2
0.5
1.33333333333333
-2.25
4.5
-1.11111111111111
7
-7
-0.166666666666667
1.5
1.14285714285714
0.5
-7
-1.75
-2
0.8
2.25
-0.428571428571429
-1.5
5
-1.16666666666667
-0.5
1.14285714285714
0
8
1.25
0.666666666666667
-0.111111111111111
5
-0.2
-2
-0.75
0.6
0.111111111111111
-1.66666666666667
-1.8
2
2.66666666666667
-1.66666666666667
8
0.75
-9
-0.333333333333333
2.5
-3
-2
-0.25
-0.285714285714286
0.7
0.142857142857143
1.25
-2.66666666666667
-9
-0.5
0.8
-4
0.111111111111111
-0.5
5
-1.8
0.857142857142857
4.5
0
-1.6
-4.5
-0.125
-1.25
-0.833333333333333
1.11111111111111
0.1
0.333333333333333
-1.33333333333333
0.375
9
2.66666666666667
Press any key to continue . . .



Not sure how 'random' it is [lol]

Share this post


Link to post
Share on other sites
Rob Loach    1504
To get an effective range value, you can subract the min from the max, times it by the NextDouble and then add the minimum:


public class MyMath
{
private static Random m_Rand = new Random();

public static double RandomNumber(double min, double max)
{
return (max - min) * m_Rand.NextDouble() + min;
}
}



The following will return a random number between 1.3 and 3.7. A free cookie to the one who guesses why I choose those number:

MyMath.RandomNumber(1.3, 3.7);

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Quote:
Original post by Rob Loach
To get an effective range value, you can subract the min from the max, times it by the NextDouble and then add the minimum:

*** Source Snippet Removed ***
The following will return a random number between 1.3 and 3.7. A free cookie to the one who guesses why I choose those number:

MyMath.RandomNumber(1.3, 3.7);


Very nice! Because it's a 1337 example? [lol] (Couldn't think of a logical explanation for 3/17 fitting into that so, eh)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this