Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
Edited 22-Nov-12 19:59pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

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. Smile | :)
 
—SA
  Permalink  
Comments
shweta89 at 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 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 23 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid