blah = CInt(Int((7 * Rnd()) + 0))
Generates a random integer between 0 and 6.
How can I modify this to give me a random number with 2 decimal places, still between 0 and 6?
As suggested below, I'm now using this code, and it seems to work:
Dim prng As New Random
Private Function aRand() As Double
Return Math.Round(prng.Next(0, 601) / 100, 2)
End Function
currentApp.statements(Pick, 7) = aRand()
currentApp.statements(Pick, 8) = aRand()
Thanks for all the suggestions.
To do this, follow these steps: Click on the cell where you'd like to generate your random number. Enter the formula =RAND()*([UpperLimit]-[LowerLimit])+[LowerLimit]. For example, if you'd like to generate a random decimal between one and 10, you may enter =RAND()*(10-1)+1.
Generate numbers with a decimal In this example, the RANDBETWEEN function generates random numbers between 2 and 4 with a decimal. Since it actually can't generate numbers with decimals the function generates values between 20 to 40.
If you want to use decimals, you will have to use a different but similar function. Start by typing "=rand()". Next, you multiply this by the difference between the limits and add the lower limit. For example, if you want to generate a random number between 100 and 110, you must type "=rand()*10+100".
Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.
Like this
Dim prng As New Random
Private Function aRand() As Double
Return prng.Next(0, 601) / 100
End Function
note that the location of the random.
Your code would look like
currentApp.statements(Pick, 7) = aRand()
currentApp.statements(Pick, 8) = aRand()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With