Results 1 to 5 of 5

Thread: How do you assign a random number to a an integer variable

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Angry

    Code:
    Randomize
            A = Rnd(9)
            B = Rnd(9)
            C = Rnd(9)
            D = Rnd(9)
            E = Rnd(9)
            F = Rnd(9)
            G = Rnd(9)
            H = Rnd(9)
    DoEvents
    That's what I have and all it gives me is 0 and 1
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  2. #2
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Code:
    Randomize(now())
            A = int(Rnd *9)+1
            B = int(Rnd *9)+1
            C = int(Rnd *9)+1
            D = int(Rnd *9)+1
            E = int(Rnd *9)+1
            F = int(Rnd *9)+1
            G = int(Rnd *9)+1
            H = int(Rnd *9)+1
    DoEvents
    There ya go, hope that helps
    -Excalibur

  3. #3
    Guest
    Plain old Randomize will work for your first statement.

  4. #4
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    True true, I just like giving it a seed that isn't necessarily the same each time a random number is needed heh
    -Excalibur

  5. #5
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Unless you're attached to your "A,B,C,etc."

    You may find this code a little more efficient.
    Code:
        Dim iArray(7) As Integer
        Dim iTemp As Integer
        
        Randomize
        For iTemp = LBound(iArray) To UBound(iArray)
            iArray(iTemp) = Int(Rnd * 9) + 1
        Next
    All the best.

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