|
-
Mar 30th, 2000, 07:16 PM
#1
Thread Starter
Hyperactive Member
I have two short queries. I tried a search on the VB database, but didn't have much success.
The following code gives me a random number between 0 and 10 in my textbox:
Private Sub Form_Load()
Randomize
Text1 = Int(Rnd * 10)
End sub
How can I exclude the number zero from appearing?
Secondly, I want to add the letter 'X' onto the number that appears in the text box. I thought it was simply a case of:
Label2.caption = Int(Rnd * 10) + "X".
But it didn't work.
Any help on these issues would be greatly appreciated.
-
Mar 30th, 2000, 07:31 PM
#2
Junior Member
This should give you no zero:
Private Sub Form_Load()
Randomize
Text1 = Int(Rnd * 9) + 1
End sub
And this should give you 9X, 3X etc
Label2.caption = Int(Rnd * 10) & "X"
-
Mar 30th, 2000, 07:54 PM
#3
Hyperactive Member
Almost right browner
Sorry browner, you are right in all points but one 
You shall use Text1 = Int(Rnd * 10) + 1 since 10 is how many numbers you want the rnd to go between.
If you use (rnd*9)+1 you will only get #1-9
but (rnd*10)+1 will give you #1-10
good luck
Onerrorgoto
Dont be to optimistic, the light at the end of the tunnel might be a train
-
Mar 30th, 2000, 08:03 PM
#4
Junior Member
How right you are. So that means Int(Rnd * 10)
actually returns one of 11 random numbers. I wish VB was less irregular in it's use of 0 or 1 as the staring point in arrays, counts, string functions, etc, etc, etc. But anyway....
-
Mar 30th, 2000, 08:29 PM
#5
Frenzied Member
browner, theres nothing irregular about Rnd() it produces a single between 0 and 1
int() of a number between 0 and 1 will ALWAYS be 0!
-
Mar 30th, 2000, 08:50 PM
#6
Junior Member
Alright - that is fair enough - but my point was that in C or other languages you are never in doubt that EVERYTHING runs from zero to...... However in VB some things are not so
i.e.
MsgBox Mid("HELLO", 0, 1) = an error whereas
MsgBox List1.List(0) = not an error
so if you have 10 characters in a string you have to do a loop from 1 to 10 to access them, however to access ten strings in a listbox you have to go from 0 to 9, to access 10 elements. In an array, you have a choice depending on your option base etc etc etc. So it is never immediately obvious that you start at 1 or 0. Ahh man - I am just getting myself in trouble here... leave me alone. I know that C uses arrays for strings and option base is optional and everything else, but still it is a bit confusing in VB.
Phew
-
Mar 30th, 2000, 09:11 PM
#7
Thread Starter
Hyperactive Member
Thanks a lot, lads.
I'm never too sure of that rnd stuff myself. I had no excuses for that & "x" instead of + "x", though - I should have figured it out a mile away.
Thanks.
-
Apr 10th, 2000, 03:42 PM
#8
Frenzied Member
Browner,
Alright - that is fair enough - but my point was that in C or other languages you are never in doubt that EVERYTHING runs from zero to...... However in VB some things are not so
Yeah, I agree totally
Listboxes run from 0
listViews run from 1
where's the logic in that??
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
|