Results 1 to 3 of 3

Thread: Generate random number using VB

  1. #1
    Join Date
    Jun 2009
    Posts
    56

    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?

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    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

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    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.

Similar Threads

  1. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  2. Generate Random Text In Ms Word
    By Captain Carrot in forum Windows Software
    Replies: 6
    Last Post: 04-09-2010, 05:58 AM
  3. How to use "Math.random()" to generate a random number in JavaScript?
    By Silent~Kid in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 05:06 AM
  4. Generate Random Password In PHP
    By Aaquib in forum Software Development
    Replies: 2
    Last Post: 23-01-2009, 10:33 PM
  5. How to Generate Random Password in vb.net
    By Aaprichit in forum Software Development
    Replies: 1
    Last Post: 24-12-2008, 07:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,853,494.49914 seconds with 17 queries