VB.Net/Development/Random

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

Generate a Random Number

 
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


Generating Random Numbers between the optional Low and High parameters

  
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


Random number

 
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


Returns a random number between the optional Low and High parameters

  
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