Randomizing an array

前端 未结 3 1682
栀梦
栀梦 2021-01-22 07:57

I wish to implement the Dr.D.E.Knuth\'s Subtractive RANDOM number generation algorithm. I wish to implement an ATM panel on which when very time user log-in the buttons will be

3条回答
  •  萌比男神i
    2021-01-22 08:48

    You don't need to use a cryptographically secure random number generator for that, nor do you need a separate class.

    Private Shared rng As New Random()
    
    Private Shared Function ShuffleArray(Of T)(arr() As T) As T()
        Dim left = Enumerable.Range(0, arr.Length).ToList()
        Dim result(arr.Length - 1) As T
    
        For i = 0 To arr.Length - 1
            Dim nextIndex = rng.Next(left.Count)
            result(i) = arr(left(nextIndex))
            left.RemoveAt(nextIndex)
        Next
    
        Return result
    End Function
    

提交回复
热议问题