|
-
May 24th, 2013, 08:39 AM
#19
Thread Starter
Lively Member
Re: Randomly selected item from list which have a %
 Originally Posted by .paul.
Code:
Dim greens() As Integer = Enumerable.Range(0, 7).Select(Function(i) 0).ToArray 'seven elements of one hundred assigned value 0
Dim reds() As Integer = Enumerable.Range(0, 10).Select(Function(i) 1).ToArray 'ten elements of one hundred assigned value 1
Dim blues() As Integer = Enumerable.Range(0, 25).Select(Function(i) 2).ToArray 'twenty five elements of one hundred assigned value 2
Dim yellows() As Integer = Enumerable.Range(0, 58).Select(Function(i) 3).ToArray 'fifty eight elements of one hundred assigned value 3
Dim possibles() As Integer = greens.Concat(reds).Concat(blues).Concat(yellows).ToArray 'join the four arrays
Dim x As Integer = r.Next(0, 100) 'pick a random number 0 to 99
Dim listIndex As Integer = possibles(x) 'listIndex will equal 0, 1, 2, or 3 with a 7% chance of 0, 10% chance of 1, 25% chance of 2, + 58% chance of 3
edit: to make it more random:
Code:
possibles = possibles.OrderBy(Function(i) r.NextDouble).ToArray
That works for whole numbers but I have decimal percentages. EG (0.25%, 14.3%, etc.) Is there another method than random to pick randomly.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|