Darryl Darryl - 2 months ago 7
Vb.net Question

How can I keep this random code to keep the same odds but change from 100 items to 125? It still needs to be the same percentage as the 100

I have this code and everything is working just fine. The code was figured for 100 items but now that has changed to 125 items. I need the code to keep the same percentage odds at random but with 125 instead of 100. Can anyone help explain exactly what this code is doing so I will know if the items ever change again that I will know what to do next time? Thank you!

For i As Integer = 0 To positions.Length - 1
Dim p As Integer = rndItems.Next(100)

If p < 90 Then
positions(i) = p \ 15
ElseIf p < 94 Then
positions(i) = 6
ElseIf p < 98 Then
positions(i) = 7
Else
positions(i) = 8
End If
Next

Answer

Your question as worded is not entirely clear, so my answer is based on my interpretation of what you're asking for. Adding some clarification in your question would be helpful.

Think of it this way: your test > 90 is the same as > .9 * 100, i.e. you want 90% of 100. Given that, you could do the following:

Const itemCount As Integer = 125
For i As Integer = 0 To positions.Length - 1
    Dim p As Integer = rndItems.Next(itemCount)

    If p < .90 * itemCount Then
        positions(i) = p \ 15
    ElseIf p < .94 * itemCount Then
        positions(i) = 6
    ElseIf p < .98 * itemCount Then
        positions(i) = 7
    Else
        positions(i) = 8
    End If
Next

I don't work in VB, so hopefully I haven't made a language error, but this should give you the general idea at least. You might also want to add code to do some intelligent rounding if you want to be strict.

Comments