12,457,770 members (55,302 online)
Rate this:
See more:
how to pick a random value from 0.1 to 5.0 ??
what should i do for that?

this is the code to generate random integer number ...but when i try it for double value its not working .. still give a interger value ..
help me to generate random double vale...

Dim objRandom As New System.Random( _
CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

Public Function GetRandomNumber( _
Optional ByVal Low As Integer = 1, _
Optional ByVal High As Integer = 100) As Integer
' Returns a random number,
' between the optional Low and High parameters
Return objRandom.Next(Low, High + 1)
End Function

And here’s how i use this function in code:

Dim intDiceRoll As Integer
intDiceRoll = GetRandomNumber(1, 6)
MessageBox.Show("You rolled a " & intDiceRoll.ToString)
Posted 22-Nov-12 19:58pm
Updated 22-Nov-12 19:59pm
v2

Rate this:

## Solution 1

Return the double value in the range 0d .. 1d using `System.Random.NextDouble` and then linear-transform the result into the range 1d .. 5d. Please see:
http://msdn.microsoft.com/en-us/library/system.random.nextdouble.aspx[^].

Do I even need to explain how? Elementary algebra for middle school is never bad to remember.

—SA
shweta89 23-Nov-12 12:03pm

i want to change in this code ..

Dim objRandom As New System.Random( _
CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

like in this code system.Int32.MaxValue will take integer value.. but i want decimal value ..so what should i do ?
Sergey Alexandrovich Kryukov 23-Nov-12 13:33pm

Now, the initialization of Random you show here is just the seeding. It is unrelated to double return and does not have to be double! There are not constructors to use double, just because there is absolutely no need in them.
--SA

Top Experts
Last 24hrsThis month
 OriginalGriff 393 Maciej Los 165 Mehdi Gholam 145 ppolymorphe 141 Dave Kreskowiak 125
 OriginalGriff 6,991 ppolymorphe 4,156 Maciej Los 3,943 Karthik Bangalore 3,665 Richard Deeming 2,945