Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
maths problem with percentage
#1
I am little confused how to go about this one.


i am want to select a block from a set of four blocks. but they each have a different weightas in how many times they get selected bassed on percentage.

Code: (Select All)
'I want to pick a block out of four blocks but wanting them to have a different weight meaning
'
'block 1 will be selected 23% of the time out of 4 blocks 60/256
'block 2 will be selected 19% of the time out of 4 blocks 50/256
'block 3 will be selected 3% of the time out of 4 blocks  9/256
'block 4 will be selected 0% of the time out of 4 blocks  0/256 I understand if 0 I ignore the selection
'
'being that the percentage of each block returned can be changed..
'
' sub storechar (xlocation,ylocation,block1%,block2%,block3%,block4%)
'
'
'
'
'
'variable (xlocation,ylocation)=block based on percentage
'
' end sub
Reply
#2
Here is how I did it:
Code: (Select All)
'test function by counts  wont be exactly 55, 23, 19, 3 but close
Randomize Timer

' the following test the function by doing 100 case calls and counting results
Do
    ReDim b(0 To 3): tot = 0 ' reinitialize
    For i = 1 To 100
        block = block0to3
        If block = 1 Then
            b(1) = b(1) + 1
        ElseIf block = 2 Then
            b(2) = b(2) + 1
        ElseIf block = 3 Then
            b(3) = b(3) + 1
        Else
            b(0) = b(0) + 1
        End If
    Next
    For i = 0 To 3
        Print b(i),
        tot = tot + b(i)
    Next
    Print tot
    Print "Compare to 55, 23, 19, 3, 100 (percents)"
    Print
    Sleep 8
Loop Until _KeyDown(27)

Function block0to3 ' return block number according to pmackay percents given
    r = Rnd * 100 ' r 0 to 99.99999
    If r <= 23 Then
        block0to3 = 1
    ElseIf r <= (23 + 19) Then
        block0to3 = 2
    ElseIf r <= (23 + 19 + 3) Then
        block0to3 = 3
    Else
        block0to3 = 0 ' here only to complete concept
    End If
    'block 1 will be selected 23% of the time out of 4 blocks 60/256
    'block 2 will be selected 19% of the time out of 4 blocks 50/256
    'block 3 will be selected 3% of the time out of 4 blocks  9/256
    'block 4 will be selected 0% of the time out of 4 blocks  0/256 I understand if 0 I ignore the selection
    '
End Function
55 percent is number of times block 0 is picked ie not block 1 or 2 or 3.
100 just makes sure all the options add to 100%.

If you want exactly 23%, 19%, 3% and 55% for block 0 we can do it all the time but a little more fuss.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#3
Here is 55, 23, 19, 3 all the time but different draws from shuffled deck:
Code: (Select All)
' create deck

Dim Shared deck(1 To 100)
For i = 1 To 100
    If i <= 23 Then
        deck(i) = 1
    ElseIf i <= (19 + 23) Then
        deck(i) = 2
    ElseIf i <= (3 + 19 + 23) Then
        deck(i) = 3
    Else
        deck(i) = 0
    End If
Next

Do
    ReDim b(0 To 3): tot = 0 ' reinitialize
    shuffleDeck
    For i = 1 To 100
        block = deck(i) ' draw next from deck
        Print block;
        If block = 1 Then
            b(1) = b(1) + 1
        ElseIf block = 2 Then
            b(2) = b(2) + 1
        ElseIf block = 3 Then
            b(3) = b(3) + 1
        Else
            b(0) = b(0) + 1
        End If
    Next
    Print
    For i = 0 To 3
        Print b(i),
        tot = tot + b(i)
    Next
    Print tot
    Print "Compare to 55, 23, 19, 3, 100 (percents)"
    Print
    Sleep 8
Loop Until _KeyDown(27)

Sub shuffleDeck
    For i = 100 To 2 Step -1
        Swap deck(i), deck(Int(Rnd * i) + 1)
    Next
End Sub

   
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#4
(09-04-2025, 09:40 AM)bplus Wrote: Here is 55, 23, 19, 3 all the time but different draws from shuffled deck:
Code: (Select All)
' create deck

Dim Shared deck(1 To 100)
For i = 1 To 100
    If i <= 23 Then
        deck(i) = 1
    ElseIf i <= (19 + 23) Then
        deck(i) = 2
    ElseIf i <= (3 + 19 + 23) Then
        deck(i) = 3
    Else
        deck(i) = 0
    End If
Next

Do
    ReDim b(0 To 3): tot = 0 ' reinitialize
    shuffleDeck
    For i = 1 To 100
        block = deck(i) ' draw next from deck
        Print block;
        If block = 1 Then
            b(1) = b(1) + 1
        ElseIf block = 2 Then
            b(2) = b(2) + 1
        ElseIf block = 3 Then
            b(3) = b(3) + 1
        Else
            b(0) = b(0) + 1
        End If
    Next
    Print
    For i = 0 To 3
        Print b(i),
        tot = tot + b(i)
    Next
    Print tot
    Print "Compare to 55, 23, 19, 3, 100 (percents)"
    Print
    Sleep 8
Loop Until _KeyDown(27)

Sub shuffleDeck
    For i = 100 To 2 Step -1
        Swap deck(i), deck(Int(Rnd * i) + 1)
    Next
End Sub
this is great. thank you bplus
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm looking for suggestions to solve the problem! Petr 10 649 02-05-2026, 04:56 PM
Last Post: ahenry3068
  Nth problem with hardware images Ikerkaz 9 469 01-23-2026, 02:58 PM
Last Post: bplus
  Install problem...... jssantoro 6 380 12-17-2025, 08:31 PM
Last Post: madscijr
  Problem with font in Teletext browser. SquirrelMonkey 7 734 08-27-2025, 11:38 AM
Last Post: BDS107
  LAN detection problem eoredson 0 339 05-20-2025, 04:48 AM
Last Post: eoredson

Forum Jump:


Users browsing this thread: