TechArena Community

TechArena Community (
-   Software Development (
-   -   Generate random number using VB (

Bambina 02-09-2009 10:42 PM

Generate random number using VB
I want to a small help with VB coding. My task is to generate a random number and display it on label when a user hits a button. I know i have to code for this in Button1_Click() method but really confused about the coding part. How can I generate the random number using VB?

kelfro 02-09-2009 10:48 PM

Re: Generate random number using VB

Private Sub Random(num As Integer, randlist() As Integer)


Dim i As Integer, j As Integer, k As Integer

For i = 1 To num
  randlist(i) = i
Next i

For j = num To 2 Step -1
  i = Int(Rnd * j) + 1
  k = randlist(j)
  randlist(j) = randlist(i)
  randlist(i) = k
Next j
End Sub

Zecho 02-09-2009 10:56 PM

Re: Generate random number using VB
Use the below random function to generate the random number:


Public Function rand(ByVal Low As Long, ByVal High As Long) As Long
  rand = Int((High - Low + 1) * Rnd) + Low
End Function

Now call this function every time you want to calculate the random number.

All times are GMT +5.5. The time now is 03:55 PM.