Website design and marketing news, hints and tips
Generating a random number (integer) in ASP.NET
For one of my sites (Eeliz.com) I needed to randomly position an image either left or right. For this I used the following random number function :
Public Function RandomNum(ByVal MaxNum As Integer, _
Optional ByVal MinNum As Integer = 0) As Integer
‘initialise the number generator
Dim r As New Random(System.DateTime.Now.Millisecond)
’some validation on the values passed in
‘on exception will return 0
If MinNum > MaxNum Then
Dim x As Integer = MinNum
MinNum = MaxNum
MaxNum = x
End If
Return r.Next(MinNum, MaxNum)
End Function
For my purpose I call the function like this – Random(2) 0 this will return either 0 or 1 randomly
You could do this – Random (21,9) – this will return a number between 10 and 20 at random