Results 1 to 30 of 30

Thread: [RESOLVED] Randomly selected item from list which have a %

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    70

    Resolved [RESOLVED] Randomly selected item from list which have a %

    Basically, I know how to get a random number but I'm not sure how to select an item from a list which has a percentage of being randomly selected.

    EG. If this is my list

    Red - 10%
    Blue - 25%
    Green - 7%
    Yellow - 58%

    I then declare a random which selects an item based off their percentage. So yellow is likely to be selected and green the least likely.

    How would I go around doing that.



    [RESOLVED]

    I used some of these posts solutions and wrote the code myself. Its minimal which i like and it works on bigger decimal probabilities.

    Code:
     Dim useRandom As New Random
            Select Case (Math.Round(useRandom.NextDouble(), 2))
                Case 0 To 0.25
                    MsgBox("Red")
                Case 0.26 To 0.5
                    MsgBox("Blue")
                Case 0.51 To 0.75
                    MsgBox("Green")
                Case 0.76 To 1
                    MsgBox("Yellow")
            End Select
    Last edited by xx_dan_xx; May 24th, 2013 at 09:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width