locked
filling an array with random numbers RRS feed

  • Question

  • I've declared a an array as

    dim addvalues(19,1) as integer

    and now need to fill it with randomly generated numbers within a predetermined range. my question is how do I do this as I can only find ways of doing it with a normal array and not a multi one.

    this is what I've started to build

          
     Dim addvalues(19, 1) As Integer
    
    
      Dim smallestnum As Integer = 1
            Dim largestnum As Integer = 10
    
            For i = 0 To 19
                addvalues(i, i) = Rnd(smallestnum, largestnum)
            Next

    Tuesday, October 25, 2016 3:44 PM

Answers

  • For such arrays you can use two loops:

    For i = 0 To 19
        For j = 0 To 1
            addvalues(i, j) = . . .
        Next
    Next

    • Proposed as answer by Mr. Monkeyboy Tuesday, October 25, 2016 6:40 PM
    • Marked as answer by josh_- Wednesday, October 26, 2016 4:22 PM
    Tuesday, October 25, 2016 5:13 PM
  • Take a look at this Function:

        Private r As New Random
        Private Function Random2DArray(ByVal uBounds1 As Integer, ByVal uBounds2 As Integer, ByVal smallestValue As Integer, ByVal largestValue As Integer) As Integer(,)
    		'Throw exceptions if there is something wrong with the arguments
    		If uBounds1 < 1 OrElse uBounds2 < 1 Then
                Throw New ArgumentOutOfRangeException("uBounds1 and uBounds2 must be greater than 0.")
            End If
            If smallestValue > largestValue Then
                Throw New ArgumentOutOfRangeException("The smallestValue must be greater than the largestValue must be greater than 0.")
            End If
    
    		'Declare a 2D array to return
            Dim values(uBounds1, uBounds2) As Integer
            largestValue += 1
    		
    		'Loop from 1 to the upper-bounds of both dimensions of the array
            For x As Integer = 1 To uBounds1
                For y As Integer = 1 To uBounds2
                    values(x, y) = r.Next(smallestValue, largestValue)
                Next
            Next
    
    'Return the 2D array
            Return values
        End Function

    Fiddle: https://dotnetfiddle.net/0FeVY1

    • Marked as answer by josh_- Wednesday, October 26, 2016 4:22 PM
    Tuesday, October 25, 2016 7:05 PM

All replies

  • Josh,

    What you might do would be to create a function and in that function, set up a List(Of Integer) then populate it with random values.

    Once done, let the return value be your list.ToArray.


    "Everybody in this country should learn how to program a computer... because it teaches you how to think." (Steve Jobs)


    • Edited by Frank L. Smith Tuesday, October 25, 2016 3:56 PM ...added link to MSDN documentation
    Tuesday, October 25, 2016 3:55 PM
  • For such arrays you can use two loops:

    For i = 0 To 19
        For j = 0 To 1
            addvalues(i, j) = . . .
        Next
    Next

    • Proposed as answer by Mr. Monkeyboy Tuesday, October 25, 2016 6:40 PM
    • Marked as answer by josh_- Wednesday, October 26, 2016 4:22 PM
    Tuesday, October 25, 2016 5:13 PM
  • I've declared a an array as

    dim addvalues(19,1) as integer

    and now need to fill it with randomly generated numbers within a predetermined range. my question is how do I do this as I can only find ways of doing it with a normal array and not a multi one.

    this is what I've started to build

          
     Dim addvalues(19, 1) As Integer
    
    
      Dim smallestnum As Integer = 1
            Dim largestnum As Integer = 10
    
            For i = 0 To 19
                addvalues(i, i) = Rnd(smallestnum, largestnum)
            Next

    Hi

    Here is some code to fill multiple ListBoxes with random integers, from smallestnumber to largestnumber, and how ever many are needed (obviously, number needed needs to be less than range)

    Option Strict On
    Option Infer Off
    Option Explicit On
    Public Class Form1
        Private rand As New Random
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' smallestnumber, largestnumber, how many
            ListBox1.DataSource = GetRand(1, 20, 5)
            ListBox2.DataSource = GetRand(11, 20, 4)
            ListBox3.DataSource = GetRand(1, 5, 2)
            ListBox4.DataSource = GetRand(2, 17, 6)
            ListBox5.DataSource = GetRand(3, 7, 3)
        End Sub
        Function GetRand(smallest As Integer, largest As Integer, tk As Integer) As List(Of Integer)
            Return Enumerable.Range(smallest, largest).OrderBy(Function(n) rand.Next).Take(tk).ToList
        End Function
    End Class


    Regards Les, Livingston, Scotland

    Tuesday, October 25, 2016 6:34 PM
  • Take a look at this Function:

        Private r As New Random
        Private Function Random2DArray(ByVal uBounds1 As Integer, ByVal uBounds2 As Integer, ByVal smallestValue As Integer, ByVal largestValue As Integer) As Integer(,)
    		'Throw exceptions if there is something wrong with the arguments
    		If uBounds1 < 1 OrElse uBounds2 < 1 Then
                Throw New ArgumentOutOfRangeException("uBounds1 and uBounds2 must be greater than 0.")
            End If
            If smallestValue > largestValue Then
                Throw New ArgumentOutOfRangeException("The smallestValue must be greater than the largestValue must be greater than 0.")
            End If
    
    		'Declare a 2D array to return
            Dim values(uBounds1, uBounds2) As Integer
            largestValue += 1
    		
    		'Loop from 1 to the upper-bounds of both dimensions of the array
            For x As Integer = 1 To uBounds1
                For y As Integer = 1 To uBounds2
                    values(x, y) = r.Next(smallestValue, largestValue)
                Next
            Next
    
    'Return the 2D array
            Return values
        End Function

    Fiddle: https://dotnetfiddle.net/0FeVY1

    • Marked as answer by josh_- Wednesday, October 26, 2016 4:22 PM
    Tuesday, October 25, 2016 7:05 PM
  • Hi josh_-,

    I suggest you could use RANDOM. This simple demo loads a random number into each array location and then display the contents of the array and random numbers from 1 to 100.

    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim random As New Random(Now.Millisecond)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim array() As Integer = {}
    
            For I As Integer = 1 To 100
                ReDim Preserve array(I - 1)
                array(I - 1) = random.Next(0, 101)
            Next
            MsgBox(arrToStr(array))
        End Sub
        Function arrToStr(arr As Integer()) As String
            Dim sb As New System.Text.StringBuilder
            For i As Integer = 0 To UBound(arr)
                If i = UBound(arr) Then
                    sb.Append(arr(i).ToString)
                Else
                    sb.Append(arr(i).ToString & ", ")
                End If
            Next
            Return sb.ToString
        End Function
    End Class

    Hope it's helpful to you.

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 26, 2016 9:55 AM
  • A generalized method to fill a two dimensional array

        Private prng As New Random
        Private Sub FillArray(arrayToFill As Integer(,), low As Integer, high As Integer)
    
            For x As Integer = 0 To arrayToFill.GetUpperBound(0)
                For y As Integer = 0 To arrayToFill.GetUpperBound(1)
                    arrayToFill(x, y) = prng.Next(low, high + 1) 'inclusive of high
                Next
            Next
    
        End Sub

    and then your code looks like

            Dim addvalues(19, 1) As Integer
            FillArray(addvalues, 1, 10)
    note that I assumed you wanted values 1 to 10 inclusive to appear in the array.  Random.Next is exclusive of the upper bound.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.



    • Edited by dbasnett Wednesday, October 26, 2016 11:12 AM
    Wednesday, October 26, 2016 11:09 AM