"Random" Value Occurances

scott_n_phnx

Active Member
Joined
Sep 28, 2006
Messages
445
I am working on a macro that will "randomly" choose between 6 values. The code that I have works so far, but I want to see if there is a way to set how often certain numbers occur. I want the higher numbers to occur less often. Any help would be appreciated. Here is what I have:
Code:
Sub Randomize()

Dim intLowNumber As Integer
Dim intHighNumber As Integer
Dim intNumber As Integer
Dim intValue As Integer

intLowNumber = 1
intHighNumber = 6

'Randomize
intNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)

Select Case intNumber
    Case 1
        intValue = "400"
    Case 2
        intValue = "600"
    Case 3
        intValue = "800"
    Case 4
        intValue = "1000"
    Case 5
        intValue = "1200"
    Case 6
        intValue = "2000"
End Select

    Sheet1.TextBox1.Value = intValue
    
    
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Interesting problem - a solution I thought of is to attach a weighting and adjust the random value range given this weighting:

Code:
Sub Randomize()
Dim intLowNumber As Integer
Dim intHighNumber As Integer
Dim intNumber As Integer
Dim intValue As Integer
Dim probValue As Integer
Dim probAdjust As Integer
intLowNumber = 1
intHighNumber = 6
probAdjust = 2
 
'Randomize with probability weighting for lower numbers
probValue = Rnd
If probValue < 0.8 Then
    intNumber = Int((intHighNumber - probAdjust - intLowNumber + 1) * Rnd + intLowNumber)
Else
    intNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
End If
 
Select Case intNumber
    Case 1
        intValue = "400"
    Case 2
        intValue = "600"
    Case 3
        intValue = "800"
    Case 4
        intValue = "1000"
    Case 5
        intValue = "1200"
    Case 6
        intValue = "2000"
End Select
Sheet1.TextBox1.Value = intValue 
End Sub

Basically, I'm setting a threshold of 0.8 and if a random value is below this, then I'm reducing the range of integer values (i.e. between 1 and 4 in this case) your random number generator can select from. Above 0.8 and it will select any value between 1 and 6

You should be able to see what I've done and adjust to suit your needs. Hope it helps
 
Upvote 0
Thanks JackDanIce, that was just what I was looking for. Unfortunately, JackBean, the No Fair thread wasn't quite what I was needing, but thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,215,216
Messages
6,123,669
Members
449,114
Latest member
aides

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top