请参考:
Sub test()
    Dim w1 As String, w2 As String, i As Integer, k As Integer
    w1 = Replace(String(10, "a"), "a", "BPBF") ''20B+20P+10F
    Randomize
    Do
        If Len(w1) = 1 Then Exit Do
        i = Int(Rnd * Len(w1)) + 1
        w2 = w2 & Mid$(w1, i, 1)
        Mid$(w1, i, 1) = "@"
        w1 = Replace(w1, "@", "")
    Loop
    w2 = w2 & w1
    Debug.Print w2
End Sub

解决方案 »

  1.   

    Private Sub Command1_Click()
    Dim i As Integer, n As Long, strString As String    List1.Clear    For i = 1 To 10
            List1.AddItem "B"
            List1.AddItem "B"
            List1.AddItem "P"
            List1.AddItem "F"
        Next i
        
        Randomize
        With List1
        Do Until .ListCount = 1
            n = Int(Rnd * .ListCount)
            strString = strString & .List(n)
            .RemoveItem n
        Loop
        strString = strString & .List(0)
        .RemoveItem 0
        End With
        
        Debug.Print strString
        MsgBox strString
    End Sub
      

  2.   

    另一种方法,可能有空跑的循环:
    Private Sub Command1_Click()
    Dim B_Count As Integer, F_Count As Integer, P_Count As Integer
    Dim strString As StringB_Count = 20
    F_Count = 10
    P_Count = 10Randomize
    Do Until Len(strString) = 40
        Select Case Int(Rnd * 3)
            Case 0
                If B_Count Then
                    B_Count = B_Count - 1
                    strString = strString & "B"
                End If
            Case 1
                If F_Count Then
                    F_Count = F_Count - 1
                    strString = strString & "F"
                End If
            Case 2
                If P_Count Then
                    P_Count = P_Count - 1
                    strString = strString & "P"
                End If
        End Select
    LoopDebug.Print strString
    End Sub
      

  3.   

    洗牌算法:Private Sub Command1_Click()
    Dim strString As String, bytItem() As Byte
    Dim i As Integer, n As Long, bytTmp As BytestrString = String(20, "B") & String(10, "F") & String(10, "P")
    bytItem = StrConv(strString, vbFromUnicode)
    For i = 0 To UBound(bytItem)
        n = Int(Rnd * (UBound(bytItem) + 1))
        bytTmp = bytItem(n)
        bytItem(n) = bytItem(i)
        bytItem(i) = bytTmp
    Next istrString = StrConv(bytItem, vbUnicode)Debug.Print strString
    End Sub