How to initialize a stack with random numbers in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Class StackInit
    Private Shared Function InitStack(ByVal size As Integer, ByVal bound As Integer) As Stack(Of Integer)
        Dim stack As Stack(Of Integer) = New Stack(Of Integer)()
        Dim random As Random = New Random()

        For i As Integer = 0 To size - 1
            stack.Push(random.Next(1, bound + 1))
        Next

        Return stack
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim stack As Stack(Of Integer) = InitStack(15, 100)

        For Each num As Integer In stack
            Console.Write(num & " ")
        Next
    End Sub
End Class



' run:
'
' 52 77 47 4 14 92 43 22 33 63 74 50 98 14 100 
'

 



answered Oct 11, 2025 by avibootz
...