• Log in or sign up for an account here: https://www.sythe.org/login

need help on random numbers

  • Thread starter Thread starter xevenx
  • Start date Start date
X

xevenx

Guest
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!
 
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. :)
 
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:
 
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
 
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!
 
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. <_<
 
Runescape Gamblingrunescape gambling
Back
Top