当前位置: 动力学知识库 > 问答 > 编程问答 >

vb.net - Randomizing an array

问题描述:

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 scrambled. every button will change its position.

Here is my code:

Public Sub addbutton()

Dim n As Integer = 0

For i As Integer = 0 To 10

' Initialize one variable

btnArray(i) = New System.Windows.Forms.Button

Next i

While (n < 10)

With (btnArray(n))

.Tag = n + 1 ' Tag of button

.Width = 40 ' Width of button

.Height = 40

FlowLayoutPanel1.Controls.Add(btnArray(n))

.Text = Chr(n + 48)

AddHandler .Click, AddressOf Me.ClickButton

n += 1

End With

End While

End Sub

Then, for sending information to button Text, I used:

 Dim btn As Button = sender

TextBox1.Text += btn.Text

Now the major task is to shuffle the btnArray() with Random() function.. But I've failed to do this. I managed to get some code for shuffling the array like follows:

Imports System.Security.Cryptography

Public Class ArrayUtilities

Private Random As RNGCryptoServiceProvider = New RNGCryptoServiceProvider

Private Bytes(4) As Byte

Public Function ShuffleArray(ByVal argArr As Array) As Array

Dim FirstArray As New ArrayList(argArr)

Dim SecoundArray As Array = Array.CreateInstance(GetType(Object), FirstArray.Count)

Dim intIndex As Integer

For i As Integer = 0 To FirstArray.Count - 1

intIndex = RandomNumber(FirstArray.Count)

SecoundArray(i) = FirstArray(intIndex)

FirstArray.RemoveAt(intIndex)

Next

FirstArray = Nothing

Return SecoundArray

End Function

Private Function RandomNumber(ByVal argMax As Integer) As Integer

If argMax <= 0 Then Throw New Exception

Random.GetBytes(Bytes)

Dim intValue As Integer = (BitConverter.ToInt32(Bytes, 0)) Mod argMax

If intValue < 0 Then intValue = -intValue

Return intValue

End Function

End Class

Module Module1

Sub Main()

Dim AU As ArrayUtilities

AU = New ArrayUtilities

Dim GivenArray As Integer() = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Dim NewArray As Array = AU.ShuffleArray(GivenArray)

Dim i As Integer

Dim stb As New System.Text.StringBuilder

stb.Append("GivenArray = {0,1,2,3,4,5,6,7,8,9}")

stb.Append(vbCrLf)

stb.Append("NewArray = {")

For i = 0 To NewArray.Length - 2

stb.Append(NewArray(i).ToString)

stb.Append(", ")

Next

stb.Append(NewArray(NewArray.Length - 1).ToString)

stb.Append("}")

Console.Write(stb.ToString)

Console.Read()

End Sub

End Module

Yet, when we debug this code we get RANDOMNESS for the array. In the same way, I want randomness for my buttons on form.

Thank you sir. sir i tried the code that u suggested.

I was missing the incremental character "n".

Tthe debug-able code is

flowlayoutpannel.controls.add(out(n)).

but it is not working as i wanted and ABOVE code is just to show the way i want 2 shuffle the BUTTONS.

Is there any simpler way to jst create an button array and randomize them with RANDOM() and adding to form.

My buddies says you are so stupid that u are working on this topic from last 20-25DAYS

网友答案:

There are quite some "shuffle" algorithms (search Fisher-Yates for example) and they are usually not difficult to implement. The EASIEST (imho) way, ist to use LINQ:

Dim r As New Random
Dim out = (From b In btnArray Order By r.Next Select b).ToArray

Maybe the question is not clear: Do you want to shuffle the POSITIONS of the buttons or do you want to shuffle the CONTENT (buttons) of the Array?

网友答案:

To set the position of the buttons randomly in the panel, instead of using n from 0 to 9 in the loop, you can use a random value from 0 to 9. Be sure you don't use the same value twice.

网友答案:

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
分享给朋友:
您可能感兴趣的文章:
随机阅读: