VB.Net/Development/Random

Материал из VB Эксперт
Перейти к: навигация, поиск

Generate a Random Number

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main(ByVal args As String())
       Dim random As New System.Random()
       Console.WriteLine(random.Next(10))
   End Sub
   

End Class


 </source>


Generating Random Numbers between the optional Low and High parameters

<source lang="vbnet"> Module MainClass

   Dim objRandom As New System.Random(System.DateTime.Now.Ticks Mod System.Int32.MaxValue)
   Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
       Return objRandom.Next(Low, High + 1)
   End Function

End Module


 </source>


Random number

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main()
       Dim objRandom As New Random()
       Dim intRandomNumber As Integer = 0
       "Process the loop until intRandomNumber = 10
       Do Until intRandomNumber = 10
           "Get a random number between 0 and 24
           intRandomNumber = objRandom.Next(25)
           "Add the number to the list
           System.Console.WriteLine(intRandomNumber)
       Loop
   End Sub

End Class


 </source>


Returns a random number between the optional Low and High parameters

<source lang="vbnet"> Public Class MainClass

   Public Shared objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
   Public Shared Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
       Return objRandom.Next(Low, High + 1)
   End Function
   Public Shared Sub Main()
       Dim intDiceRoll As Integer
       intDiceRoll = GetRandomNumber(1, 6)
       System.Console.WriteLine(intDiceRoll.ToString)
   End Sub

End Class


 </source>