Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
Updated 22-Nov-12 19:59pm
v2

1 solution

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
 
Share this answer
 
Comments
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    
I answered! Please read carefully. If you did not get it, please explain what exactly is not clear.
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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900