Results 1 to 3 of 3

Thread: Quite Easy!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329

    Question

    I need vb to generate a random integer between 0 and 8 ( including 0 and 8 though)
    ______________

  2. #2
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    Code:
    Dim intRandom as integer
    Randomize
    retVal = Int((9 * Rnd) + 1)
    If retVal = 1 Then intRandom = 0
    If retVal = 2 Then intRandom = 1
    If retVal = 3 Then intRandom = 2
    If retVal = 4 Then intRandom = 3
    If retVal = 5 Then intRandom = 4
    If retVal = 6 Then intRandom = 5
    If retVal = 7 Then intRandom = 6
    If retVal = 8 Then intRandom = 7
    If retVal = 9 then intRandom = 8
    NXSupport - Your one-stop source for computer help

  3. #3
    Guest
    A bit more...

    Code:
    Public Function GenerateRandom(minVal As Long, maxVal As Long) As Long
        intr = -1
        maxVal = maxVal + 1
    
    
        If maxVal > 0 Then
    
    
            If minVal >= maxVal Then
                minVal = 0
            End If
        Else
            minVal = 0
            maxVal = 10
        End If
        Randomize (DatePart("s", Now) + DatePart("m", Now))
    
    
        Do While (intr < minVal Or intr = maxVal)
            intr = CLng(Rnd() * maxVal)
        Loop
        GenerateRandom = intr
    End Function
    
    Usage:
    
    Debug.Print GenerateRandom(0, 8)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width