There is a Application.Worksheetfunction.RandBetween(0, 1), but it only returns a whole number: 0 or 1, while I need a not whole numberr between 0 and 1.
What is a equivalent of worksheet function Rand()?
The worksheet function should be =RAND(0,10000)/10000. You can adjust the number of decimal places in this way.
The VBA equivalent might look like this function. Determine the number of digits by means of the argument.
Function RandomNumber(ByVal Digits As Integer) As Double
Dim Fun As Double
Randomize
Fun = Rnd
RandomNumber = Round(Fun, Digits)
End Function
I have written this Function pretty long ago and if I need a random number i normally can generate it easily this way.
Note that you need the Randomize-Statement to make sure, that the random creates a new set of random numbers each time you use the function.
Private Function random(minval As Integer, maxval As Integer, _
Optional decimalaccuracy As Integer = 10000) As Double
Randomize
random = (maxval - minval + 1) * Rnd + minval
If decimalaccuracy <> 10000 Then random = _
Application.WorksheetFunction.Round(random, decimalaccuracy)
End Function
try this
Sub rndNumber()
Dim rNum As Double
Randomize
rNum = Format(Rnd(1), "#0.00")
End Sub
check randomise function