Tuesday 6 November 2012

How to generate random number in asp.net vb



Partial Class Demo
    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim intDiceRoll As Integer
        Dim intDicRoll2 As Integer
        intDiceRoll = GetRandomNumber(10, 96)

        intDicRoll2 = GetRandomNumber2(99, 199)



        Label1.Text = intDicRoll2 & intDiceRoll.ToString


    End Sub

    Dim objRandom As New System.Random( _
  CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

    Public Function GetRandomNumber( _
      Optional ByVal Low As Integer = 1, _
      Optional ByVal High As Integer = 99) As Integer
        ' Returns a random number,
        ' between the optional Low and High parameters
        Return objRandom.Next(Low, High + 1)
    End Function

    Public Function GetRandomNumber2( _
      Optional ByVal Low2 As Integer = 99, _
      Optional ByVal High2 As Integer = 199) As Integer
        ' Returns a random number,
        ' between the optional Low and High parameters
        Return objRandom.Next(Low2, High2 + 1)
    End Function


End Class

0 comments:

Post a Comment


                                                            
 
Design by Abhinav Ranjan Sinha