vendredi 24 septembre 2021

Does shuffling the elements of an array on top of a code that randomly picks an element in an array increases randomness?

OS: Windows 10 Pro Version 20H2 (OS Build: 19042.1237)
Application: Microsoft 365 Version 2108

I just finished creating a custom function that shuffles an array of values and generates a random string from it but then this question struck me. Does shuffling the elements of an array increases (or decreases) randomness? Would I be better off without it and why? The code already picks elements randomly even without shuffling. Here's a basic concept of what I've done.

Sub GenerateRandomString()
 
'Variable declaration
    Dim ArrNums() As Integer
    Dim i As Integer
    Dim rNum As Integer
    Dim Cache As Integer
    Dim RandomString As String
    Dim ShuffleSwitch As Integer
    
    'Switch for the shuffle for testing, 1 to turn on, 0 to turn off..
    ShuffleSwitch = 1

'Main procedure
    'Fill array with values
    ReDim ArrNums(0 To 9) As Integer
    For i = 0 To 9
        ArrNums(i) = i
    Next i
    
    Randomize
    
    'Shuffle array elements
    If ShuffleSwitch = 1 Then
        For i = LBound(ArrNums) To UBound(ArrNums)
            rNum = Int(Rnd * (UBound(ArrNums) - LBound(ArrNums) + 1) + LBound(ArrNums))
            Cache = ArrNums(i)
            ArrNums(i) = ArrNums(rNum)
            ArrNums(rNum) = Cache
        Next i
    End If

    'Generate eight random strings
    For i = 1 To 8
        rNum = Int(Rnd * (UBound(ArrNums) - LBound(ArrNums) + 1) + LBound(ArrNums))
        RandomString = RandomString & ArrNums(rNum)
    Next i

'Debug code
    Debug.Print RandomString

'Housekeeping
    Erase ArrNums
    i = vbEmpty
    rNum = vbEmpty
    Cache = vbEmpty
    ShuffleSwitch = vbEmpty
    RandomString = vbEmpty

'End of the line indicator
    Debug.Print "alright.."

End Sub

I tried researching about this subject on the internet but all articles are geared towards explaining how to accomplish it.

I would appreciate any clarification from you guys. Thank you all very much..




Aucun commentaire:

Enregistrer un commentaire