Results 1 to 7 of 7

Thread: Make a random number

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Location
    Ca
    Posts
    93

    Question

    how do you make a number between 10000 and 99999 apear in a text box when you press a button?
    Timbudtwo
    I have no life, only one with computers.

    VB 6.0 Enterprise Edition
    [hr]

  2. #2
    Guest
    Here you go:

    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:
    
    Private Sub Command1_Click()
    x = GenerateRandom(10000, 99999)
    Text1.text = x
    End Sub
    (Sorry about that Gen-X)

    [Edited by Matthew Gates on 08-23-2000 at 11:39 PM]

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    You can't set the datatype of the function to "Long" and then pass it strings

    ie GenerateRandom("10000","99999")

    It should be

    GenerateRandom(10000,99999)

  4. #4
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    This might save a few CPU cycles...
    Code:
    Function GenRndmStr(minVal As Long, maxVal As Long) As String
    Dim Rng As Long, Res as Long
      Rng = maxVal - minVal
      If Rng <= 0 Then
        GenRndmStr = IIF(Rng < 0, "N/A", "0")
        Exit Function
      End If
      Res = Rnd() * Rng
      GenRndmStr = Format((minVal + Res), "#,###,###,##0")
    End Function
     
    Usage:
    
    Private Sub Command1_Click()
      Text1.text = GenRndmStr(10000, 99999)
    End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Location
    Ca
    Posts
    93
    I thought I sent a message last night, saying that I had gotten it, and thank you so much for all the people that helped out, but, all of that code isn't needed, it's just 1 line of code it's


    Code:
    Text1.text = Int( (10000 * Rnd) + 99999) ' Generate random value between 1000 and 99999.
    Timbudtwo
    I have no life, only one with computers.

    VB 6.0 Enterprise Edition
    [hr]

  6. #6
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    DON'T FORGET Randomize

    Put Randomize before you use Rnd otherwise it'll generate the same numbers whenever restarted.

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    Code:
    The line you show cannot give you a number between 1000 and 99999 unless the number generated is 0 since you are adding 99999 to the generated number...
    There is also a syntax error(100 00 * rnd)
    
    Text1.text = Int(  (100 00 * Rnd) + 99999) ' Generate random value between 1000 and 99999
    
    '<<<<<<<<<<<<<<<<<<<<<   >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    
    'produce a number between 0 and 98,999 and add 1000 to 
    'the number produced
    
    Randomize
    Text1.Text = Int((98999 * Rnd) + 1000)   ' Generate random value between 1000 and 99999
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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