ShowUsYour<Blog>

Irregular expressions regularly

Picking random guys

Ron just asked me to demo how I thought the Random guy selector would be implemented so, I thought I'd crank out a quickie demo. First, I'll create a Guy type:

Class Guy
    Private mName As String
    Private mScore As Integer
    Public Sub New(ByVal name As String, ByVal score As Integer)
        Me.mName = name
        Me.mScore = score
    End Sub
    Public ReadOnly Property Name() As String
        Get
            Return mName
        End Get
    End Property
     Public ReadOnly Property Score() As Integer
        Get
            Return mScore
        End Get
    End Property
    Public Overrides Function ToString() As String
        Return Me.mName & "  ( " & Me.mScore & " ) "
    End Function
End Class

This example populates a collection of guys on init of a process and then handles an event to randomly select a guy from the collection. Using the logic mentioned in the previous post, a guy with a higher "Score" should have a higher probability of pushing the randomly generated target below zero:

    Private mGuys As New ArrayList
    Private mSumOfGuys As Integer

    ' Initailize the guys and assign them a ranking (Score)
    Private Sub InitailizeGuys(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim names As String() = {"Jim", "Bob", "Wayne", "Foo", "Darren", "Barry"}
        Dim r As New Random
        For Each s As String In names
            Dim guy As New Guy(s, r.Next(100))
            mGuys.Add(guy)
            mSumOfGuys += guy.Score
            LabelGuys.Text &= guy.ToString & vbCrLf
        Next
    End Sub
    ' as per the method name
    Private Sub PickARandomGuy(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetRandomGuyButton.Click
        Dim r As New Random
        Dim randScore As Integer = r.Next(mSumOfGuys)
        For Each g As Guy In mGuys
            randScore -= g.Score
            If randScore <= 0 Then
                LabelRandomGuy.Text = "New guy is : " & g.ToString
                Exit Sub
            End If
        Next
    End Sub

Here's Ron's results (formatted).
I ran a test picking a "Guy" 10000000 times and here is the result:
Player Weight   Weighted Probability  Occurance (10000000 iterations)
======  ======  =====================  ===============================
Jim      20           05.87                06.34
Bob      10           02.93                03.02
Wayne    40           11.73                11.91
Foo       5           01.47                01.48
Darren   90           26.39                26.35
Barry    22           06.45                06.57
Randy    45           13.20                13.36
John     11           03.23                03.11
Doe      98           28.74                27.86
Posted: Nov 27 2003, 10:42 AM by digory | with 6 comment(s)
Filed under: ,

Comments

Todd Moon said:

OK, now set up an app that runs PickARandomGuy a few thousand times and records the guy picked each time. Then post some statistics. Basically just a chart which shows what percent of time each Guy was picked and their score. I'm curious how the their score relates to that percentage. Maybe run the test with at least twice the number of Guys so they have a smoother distribution of scores.
# November 27, 2003 12:11 AM

Ron Krauter said:


Thank you Sir for taking the time to write the Code!

I ran a test picking a "Guy" 10000000 times and here is the result:

Player Weight Weighted Probability Occurance (10000000 iterations)
====== ====== =========== =========
Jim 20 05.87 06.34
Bob 10 02.93 03.02
Wayne 40 11.73 11.91
Foo 5 01.47 01.48
Darren 90 26.39 26.35
Barry 22 06.45 06.57
Randy 45 13.20 13.36
John 11 03.23 03.11
Doe 98 28.74 27.86

So, this is perfect. The only problem I see is this section:

If randScore <= 0 Then
LabelRandomGuy.Text = "New guy is : " & g.ToString
Exit Sub
End If

What if you have a lot of "Guys", then we might end up with a lot of loops just to get randScore to 0.

Thanks again,

-ron
# November 27, 2003 12:15 AM

Ron Krauter said:

Sorry about the formating..second try
<code>
Player Weight Weighted Probability Occurance (10000000 iterations)
====== ====== =========== =========
Jim 20 05.87 06.34
Bob 10 02.93 03.02
Wayne 40 11.73 11.91
Foo 5 01.47 01.48
Darren 90 26.39 26.35
Barry 22 06.45 06.57
Randy 45 13.20 13.36
John 11 03.23 03.11
Doe 98 28.74 27.86
</code>
# November 27, 2003 12:16 AM

Darren Neimke said:


Thanks for saving me the time Ron ;-)
# November 27, 2003 12:20 AM

Ron Krauter said:

Todd,

I just saw your post after posting my findings! Although I wish my posting was better formatted.

-ron
# November 27, 2003 12:36 AM

Ron H. Krauter said:

What is the purpose for what you are doing?

# November 5, 2010 2:37 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)