Random Array in class (Object Oriented)

itsmeyouraldrin

New member
Joined
Oct 1, 2014
Messages
1
Programming Experience
Beginner
Hi, I'm currently making a game that shows random words and you have to retype the word that's displayed ...
my question is how do i make a Class that generates randon arrays??
 
What exactly do you mean by "generates random arrays"?

If you want to do something random in VB.NET then you basically start with the Random class. You create an instance and then you call the appropriate method(s) to generate one or more random numbers. You then use those numbers in whatever way is appropriate in particular scenario. If you want to create an array of random size then you call Random.Next to generate an Integer and then use that Integer as the upper bound of your array. If you want to select a random element from an array then you call Random.Next to generate an Integer and use that Integer as an index into your array. If you want to randomly order the elements of an array then you can call Random.NextDouble to generate a Double value for each element and then call Array.Sort to sort your array based on those values. Etc.

So, start thinking of your scenario in those terms. Assume that you can generate random numbers using an instance of the Random class and ask yourself how you can use those random numbers to achieve your aim.
 
Use a Shuffle Sort algorithm

I think what you are looking for is a shuffle sort algorithm, which works with an array. First you enter the data into the array. Then you shuffle the array elements to put the data into a random order without repeating or omitting any of the data items. Here is an example:

VB.NET:
Public Class Form1

    Private randarray() As String
    Private num As Integer

    Private Sub btnWords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWords.Click
        Dim snum As String
        snum = InputBox("Enter number of words in array...")
        Integer.TryParse(snum, num)
        ReDim randarray(num)

        'Assign words to array
        For x = 1 To num
            randarray(x) = InputBox("Enter word # " & x)
        Next x
    End Sub

    Private Sub btnShuffle_Click(sender As System.Object, e As System.EventArgs) Handles btnShuffle.Click
        Dim x, mix As Integer, temp As String
        Dim randnum As Random = New Random()

        'Swap the array indexes randomly
        For x = num To 1 Step -1
            mix = randnum.Next(1, x)
            temp = randarray(mix)
            randarray(mix) = randarray(x)
            randarray(x) = temp
        Next x

        'Display all the rearranged array values one by one
        For x = 1 To num
            MessageBox.Show(randarray(x))
        Next x
    End Sub

End Class
 
Solitaire said:
Dim randnum As Random = New Random()
Move this out of the Click event handler to a Private field. Random class works best when you create only one instance and use that for all random generations.
Solitaire said:
'Swap the array indexes randomly
A common "shortcut" to do this is to use Linq:
randarray = (From item In randarray Order By randnum.NextDouble).ToArray
 
Back
Top