need help on random numbers

Discussion in 'Programming General' started by xevenx, Aug 6, 2008.

need help on random numbers
  1. Unread #1 - Aug 6, 2008 at 9:46 AM
  2. xevenx
    Referrals:
    0

    xevenx Guest

    need help on random numbers

    Hey.
    I need my program to get a random number from 1-100 when I hit the commandbutton.

    BUT:
    the numbers have to be 3 "numbers". like:
    001
    010
    100

    I need the code to both VB6 and VB8 (vb.net)

    Thanks!
     
  3. Unread #2 - Aug 6, 2008 at 10:38 AM
  4. Darthatron
    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    need help on random numbers

    Use a "Rand" function in conjunction with the "Right$" function.

    Example:

    Code:
    Private Function Rand(ByVal Low As Long, ByVal High As Long) As Long
        Randomize
        Rand = CLng((High - Low + 1) * Rnd) + Low
    End Function
    To get a random number between 1 and 100:

    Code:
    Dim iNumber As Integer
    iNumber = Rand(1, 100)
    To make that number 3 "numbers" long:

    Code:
    Dim iNumber As Integer
    iNumber = Right$("000" & Rand(1, 100), 3)
    If you actually want to learn, I'll try and explain some of this stuff. :)
     
  5. Unread #3 - Aug 6, 2008 at 10:50 AM
  6. xevenx
    Referrals:
    0

    xevenx Guest

    need help on random numbers

    Ofcourse I want to learn;)
    But I didn't understand How to use it.

    Code:
    Private Function Rand(ByVal Low As Long, ByVal High As Long) As Long
        Randomize
        Rand = CLng((High - Low + 1) * Rnd) + Low
    End Function
    
    
    Private Sub Command1_Click()
    Dim iNumber As Integer
    iNumber = Right$("000" & Rand(1, 100), 3)
    Text1.Text = Rand(1, 100)
    End Sub
    This doesn't work either:

    Code:
    Private Sub Command1_Click()
        Dim iNumber As Integer
    iNumber = Right$("000" & Rand(1, 100), 3)
    Text1.Text = iNumber
    End Sub
    I failed:embar:
     
  7. Unread #4 - Aug 6, 2008 at 10:58 AM
  8. jdsfighter
    Joined:
    Jan 21, 2007
    Posts:
    603
    Referrals:
    0
    Sythe Gold:
    0

    jdsfighter Forum Addict
    Visual Basic Programmers

    need help on random numbers

    simple

    Code:
    Private Sub Command1_Click()
    Text1 = Format(Int((100 - 1 + 1) * Rnd + 1), "00#")
    End Sub
    
    Private Sub Form_Load()
    Randomize
    End Sub
    Broken Down to be easier to read

    Code:
    Private Sub Command1_Click()
        Text1 = Format(Rand(1, 100), "00#") 'Generates a random number
        'and formats it so it is always 3 digits long.
    End Sub
    
    Private Sub Form_Load()
        Randomize 'Initializes vb6's randomize timer
    End Sub
    
    Private Function Rand(lngHigh As Long, lngLow As Long) As Long
        Rand = lng((lnghight - lngLow + 1) * Rnd + lngLow) 'This is
        'a simple equation to generate a number between 2 boundaries.
    End Function
     
  9. Unread #5 - Aug 6, 2008 at 11:01 AM
  10. xevenx
    Referrals:
    0

    xevenx Guest

    need help on random numbers

    This works:

    Code:
    Private Function Rand(ByVal Low As Long, ByVal High As Long) As Long
        Randomize
        Rand = CLng((High - Low + 1) * Rnd) + Low
    End Function
    
    
    Private Sub Command1_Click()
       Text1.Text = Right$("000" & Rand(1, 100), 3)
    End Sub
    EDIT:

    using jdsfighter's first exsample! works fine...
    Thanks guys!
     
  11. Unread #6 - Aug 6, 2008 at 11:05 AM
  12. Darthatron
    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    need help on random numbers

    No, no. I failed. :p

    I was saving a string into an integer. >_>

    This will work fine: (I should really test these things before I post them. :p)

    Code:
    Private Sub Command1_Click()
        Text1.Text = Right$("000" & Rand(1, 100), 3)
    End Sub

    Now, to explain what all those pretty functions do:

    Code:
    "Rnd" this returns a random value between 0 and 1.
    "Randomize" this makes Rnd equal a different number, every time.
    "Right$" this reads an amount of letters from the right of a string, for example:
    
    msgbox Right$("OMGLOL!", 5)
    
    Would return "GLOL!" in a message box.
    
    -------
    
    "Rand" this is a very simple function that gets a random number between the "Low" and "High" values.
    
    I think you should understand the rest. :)
    
    
    
    EDIT: Damn you jdsfighter! Next time I won't go into so much detail. <_<
     
< "Selection" help, please. :S | Opening links in a webbrowser >

Users viewing this thread
1 guest


 
 
Adblock breaks this site