TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Generate random number using VB (http://forums.techarena.in/software-development/1241104.htm)

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
 
Code:

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

Randomize()

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:

Code:

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.