1

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()?

enter image description here

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Fair enough - don't know why it displayed it a minute a go
0

try this

    Sub rndNumber()
        Dim rNum As Double

             Randomize
             rNum = Format(Rnd(1), "#0.00")
    End Sub

check randomise function

Comments

0

Rnd() is the VBA equivalent of the formula RAND().

Example Usage:

Dim rand1 As Double
'Initialize the random number generator.
Randomize
rand1 = Rnd()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.