Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------- Fisher-Yates Shuffle Algorithm ------------------------------------------------
- ' https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
- 'Modern Variant 1
- ' -- To shuffle an array a of n elements (indices 0..n-1)
- 'For i from n−1 downto 1 do
- ' j ← random Integer such that 0 ≤ j ≤ i
- ' exchange a[j] And a[i]
- ' For i = _baseArr.Count - 1 To 1 Step -1
- ' Dim j As Integer = r.Next(0, i + 1)
- ' Dim temp As Integer = _baseArr(i)
- ' _baseArr(i) = _baseArr(j)
- ' _baseArr(j) = temp
- ' Next
- '-----------------------------------------------------------------------------------------------------------------------------
- 'An equivalent version which shuffles the array in the opposite direction (from lowest index to highest) Is
- 'Modern Variant 2
- '-- To shuffle an array a of n elements (indices 0..n-1)
- 'For i from 0 To n−2 Do
- ' j ← random Integer such that i ≤ j < n
- ' exchange a[i] And a[j]
- 'generate sequence number
- 'ex 3x3 or _maxlimit = 9
- 'output _baseArr() = {1,2,3,4,5,6,7,8,0}
- Dim _baseArr() As Integer = Enumerable.Range(1, _maxLimit - 1).Concat(Enumerable.Repeat(0, 1)).ToArray()
- For i = 0 To _baseArr.Count - 2
- Dim j As Integer = r.Next(i + 1, _baseArr.Count)
- Dim temp As Integer = _baseArr(j)
- _baseArr(j) = _baseArr(i)
- _baseArr(i) = temp
- Next
- '-----------------------------------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment