13,248,343 members (58,863 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 20:58pm
Updated 22-Nov-12 20: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
 Peter Leow 135 ppolymorphe 85 OriginalGriff 70 Andy Lanng 50 phil.o 50
 OriginalGriff 3,404 Karthik Bangalore 1,967 ppolymorphe 1,379 Dave Kreskowiak 1,276 CPallini 1,185