|
-
Aug 23rd, 2000, 10:24 PM
#1
Thread Starter
Lively Member
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]
-
Aug 23rd, 2000, 10:29 PM
#2
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]
-
Aug 23rd, 2000, 10:35 PM
#3
Hyperactive Member
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)
-
Aug 24th, 2000, 01:57 AM
#4
Hyperactive Member
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
-
Aug 24th, 2000, 01:31 PM
#5
Thread Starter
Lively Member
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]
-
Aug 24th, 2000, 01:35 PM
#6
So Unbanned
DON'T FORGET Randomize
Put Randomize before you use Rnd otherwise it'll generate the same numbers whenever restarted.
-
Aug 24th, 2000, 02:02 PM
#7
_______
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|