Random Numbers in C#

This topic is 4356 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 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 on other sites
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 on other sites
Quote:
 Original post by brwarnerI 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 on other sites
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.

Share on other sites
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 on other sites
Quote:
 Original post by Drew_BentonThis 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 on other sites
Quote:
 Original post by RenderTargetIt 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.8888888888888890.4285714285714292.5-4-0.87552.333333333333330.91.401.66666666666667-0.12510.222222222222222-0.50.3333333333333331.33333333333333-0.125-2.33333333333333-1.81.80-1.125-20.51.33333333333333-2.254.5-1.111111111111117-7-0.1666666666666671.51.142857142857140.5-7-1.75-20.82.25-0.428571428571429-1.55-1.16666666666667-0.51.14285714285714081.250.666666666666667-0.1111111111111115-0.2-2-0.750.60.111111111111111-1.66666666666667-1.822.66666666666667-1.6666666666666780.75-9-0.3333333333333332.5-3-2-0.25-0.2857142857142860.70.1428571428571431.25-2.66666666666667-9-0.50.8-40.111111111111111-0.55-1.80.8571428571428574.50-1.6-4.5-0.125-1.25-0.8333333333333331.111111111111110.10.333333333333333-1.333333333333330.37592.66666666666667Press any key to continue . . .

Not sure how 'random' it is [lol]

Share on other sites
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 on other sites
Quote:
 Original post by Rob LoachTo 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)