Results 1 to 4 of 4

Thread: [RESOLVED] Drawing a blank?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Resolved [RESOLVED] Drawing a blank?

    i am using this code
    VB Code:
    1. Dim i As Integer
    2.    
    3.     Randomize
    4.     i = Rnd(List1.ListCount) * List3.ListCount
    5.     Label4.Caption = List3.List(i)
    to display a random value of a list in a label. It works pretty good except ocassionally it will not put any thing in the label, leaving it blank. I was wondering if there is a solution for this?

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Drawing a blank?

    The general formula for generating a specific range of random numbers is

    Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

    So do

    VB Code:
    1. Dim i As Integer
    2.    
    3.     Randomize
    4.     i = Int(List1.ListCount +1) * Rnd
    5.     Label4.Caption = List3.List(i)

    You could simplify that if you want

    VB Code:
    1. Randomize
    2.  
    3.     Label4.Caption = List3.List(Int(List1.ListCount +1) * Rnd)

  3. #3
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Drawing a blank?

    What does List1.ListCount have to do with a random item from List3?
    VB Code:
    1. Label4.Caption = List3.List(Int(Rnd*List3.ListCount))

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Drawing a blank?

    thanks! Works much better!

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