C# Random double between min and max

How can I create an random number (double) between     .23   and  3.2?
LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Just use the generic formula:

    Random.NextDouble() * (maximum - minimum) + minimum;

Example:
        private Random R = new Random();

        private void button1_Click(object sender, EventArgs e)
        {
            double dbl = R.NextDouble() * (3.2 - .23) + .23;
            Console.WriteLine(dbl.ToString());
        }

Open in new window

0
käµfm³d 👽Commented:
Use the NextDouble method of the Random class and a little bit of math magic:

Random r = new Random();
double d = r.NextDouble();

d = ((d * (3.2 - .23)) + .23);

Open in new window


*edit

Since Idle_Mind is faster at the keys than I am, it seems all I have left is to augment my answer a bit.  (Great to see you back Idle!)

The reason this works is because NextDouble will always return a value between 0 and 1. The max - min gives the difference between the two values. If the call to NextDouble returned 0, anything multiplied by zero is zero, so the subsequent addition of the min will yield your lower bound.

Now if NextDouble returned 1, then you have the difference of max and min multiplied by 1. Anything multiplied by 1 is itself, so you end up with that difference. Adding back in the min gives you max, which is your upper bound on your range.

Anything else that NextDouble returns causes the resulting arithmetic to fall trivially in the range of min and max.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
Awesome explanation.  It's good to be back.  =)
0
AndyAinscowFreelance programmer / ConsultantCommented:
Just don't forget to check (and adjust if necessary) that the maximum entered is actually greater than the minimum entered else the above will give garbage results.
0
Erol DemirciCommented:
I believe your answers could be incorrect??? on the upper bound (Now if NextDouble returned 1, submitted by kaufmed).

C# random DOUBLE generator var.NextDouble() will generate values (0, 1], notice the bracket on the right?  It does NOT include 1, which means, his upper bound will never be reached.

Please read:
https://msdn.microsoft.com/en-us/library/system.random.nextdouble(v=vs.110).aspx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.