Results 1 to 7 of 7

Thread: sort of random number question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 1999
    Location
    France
    Posts
    90

    Hi,

    I use the following code to load random numbers, from -12 to +12, into
    an array of text boxes when my form loads. 0 is excluded - it never loads into a textbox.

    Private Sub Form_Load()
    Dim iCounter As Integer
    For iCounter = 0 To 7
    Text1(iCounter) = 0
    Randomize
    Do Until Text1(iCounter) <> 0
    Text1(iCounter) = Int((23 * Rnd) - 10)
    Loop
    Next
    End Sub

    I've been trying for ages to adjust it a little bit. For numbers greater than 0 I want the "+" character to appear before those numbers. E.g: instead of "2" in a text box, it will be "+2".

    I tried things like:

    Do Until Text1(iCounter) <> 0
    If Text1(iCounter) > 0 Then
    Text1(iCounter) = "+" & Int((23 * Rnd) - 10)
    Loop
    Next

    but had no luck. Any ideas?

    Thanks.

  2. #2
    Lively Member
    Join Date
    Jan 2000
    Location
    Omaha, Ne
    Posts
    65

    VAL

    Don't know if I can help but a text box value is a string. Perhaps if you use the val() function which converts to numeric you might get some desired results.

  3. #3
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Try,

    Code:
    Do Until Cint(Text1(iCounter).Text) <> 0 
      If Cint(Text1(iCounter).Text) > 0 Then 
      Text1(iCounter).Text = "+" & Int((23 * Rnd) - 10) 
    Loop

  4. #4
    Lively Member
    Join Date
    Jan 2000
    Location
    Springfield, IL
    Posts
    124
    Try this
    Code:
    Private Sub Form_Load() 
        Dim iCounter As Integer 
        Dim intNum as Integer
    
        For iCounter = 0 To 7 
            intNum = 0 
            Randomize 
            Do Until intNum <> 0 
                intNum = Int((23 * Rnd) - 10) 
            Loop
            Text1(iCounter) = IIF(intNum > 0,"+","-") & intNum 
        Next 
    End Sub

  5. #5
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    Try this instead:

    Code:
    Private Sub Form_Load()
       Dim iCounter As Integer
       Dim iValue As Integer
       
       For iCounter = 0 To 7
          iValue = 0
          Randomize
          
          Do Until iValue <> 0
             iValue = Int((23 * Rnd) - 10)
          Loop
          
          If iValue > 0 Then
             Text1(iCounter).Text = "+" & iValue
          Else
             Text1(iCounter).Text = iValue
          End If
       
       Next
    End Sub

  6. #6
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    Opps! Use bsmith's it's better!

    PS.
    Anybody know why we can't Edit a response anymore?
    It kind of aggrevating.

  7. #7
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658

    True

    BSmiths is better.

    But we are back on to the dodgy topic of IIF statements. Which take a lot of time.

    Seeing as if it is a negative number you don't have to insert a sign it would just be better to test for > 0

    Code:
    Private Sub Form_Load() 
        Dim iCounter As Integer 
        Dim intNum as Integer
    
        For iCounter = 0 To 7 
            intNum = 0 
            Randomize 
            Do Until intNum <> 0 
                intNum = Int((23 * Rnd) - 10) 
            Loop
    
            If intNum > 0 Then
              Text1(iCounter) = "+" & intNum
            Else
              Text1(iCounter) = intNum
            End If
        Next 
    End Sub

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