Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomizing with decimal numbers 1

Status
Not open for further replies.

tutuster

Programmer
Feb 2, 2005
41
FI
Here's the deal. I need to randomize a decimal value (or values) which is between given arguments, let's say 3.7 -6.2. Does .NET have some handy classes to take a look on, or do I need to get some other solution?

Randomizing from 3-6 works fine, but I need to have more accurate numbers here.

I've been thinking about randomizing the numbers and decimals separately (eg. between 3.7 and 6.2 I'd first randomize between 3 and 6, then the decimals from 0-9),add the numbers resulting in a decimal number, and then checking if the randomized value is less (or equal) than 6.2 and more (or equal) than 3.7, but that is a very very clumsy solution.

Any tips?
 
You could use something like

Dim objRND As Random
Dim num As Double
num = objRND.Next(6, 6) + objRND.NextDouble()
 
This seems to work:
Code:
        Dim MyValue As Double
        Randomize()
        MyValue = Math.Round((6.2 - 3.7) * Rnd() + 3.7, 2)
        MessageBox.Show(MyValue)

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Thanks for the both of you, ca8msm's code is just what I was looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top