Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random number to 2 decimal places

Tags:

vb.net

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.

like image 311
finisterre Avatar asked Jun 16 '13 14:06

finisterre


People also ask

How do I generate a random number with two decimal places in Excel?

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.

Can you do Randbetween with decimals?

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.

How do you generate random numbers with decimals?

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".

How do you convert a number to two decimal places?

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.


1 Answers

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()
like image 151
dbasnett Avatar answered Oct 24 '22 11:10

dbasnett