|
-
Mar 30th, 2006, 09:34 PM
#1
Thread Starter
New Member
easy question
what command do i use to make a random number 1-1000 pop up into a label?
-
Mar 30th, 2006, 09:39 PM
#2
Re: easy question
To generate a random number you create a Random object and call its Next method. To display something in a Label you convert it to a String, generally using its ToString method, and assign the result to the Text property of the Label. I'll let you implement code from that yourself as doing it yourself helps you learn.
-
Mar 30th, 2006, 10:06 PM
#3
Hyperactive Member
Re: easy question
randomnum = (Int((9) * Rnd()))
I think that gives 0-8 so to make it 1-1000 just do + 1 and change 9 to 1001
-
Mar 30th, 2006, 10:17 PM
#4
Re: easy question
 Originally Posted by OMITT3D
randomnum = (Int((9) * Rnd()))
I think that gives 0-8 so to make it 1-1000 just do + 1 and change 9 to 1001
That's the method inherited from VB6. The Random class is the preferred method in .NET apps. Simpler and more functional.
-
Mar 30th, 2006, 10:20 PM
#5
Hyperactive Member
Re: easy question
I am using that in an app atm and it works fine
-
Mar 30th, 2006, 10:59 PM
#6
Re: easy question
 Originally Posted by OMITT3D
I am using that in an app atm and it works fine 
I didn't say it wouldn't work. I said the preferred method in .NET apps is to use the System.Random class, which is simpler and more functional.
-
Mar 31st, 2006, 12:45 AM
#7
Re: easy question
 Originally Posted by OMITT3D
I am using that in an app atm and it works fine 
hmmm it may seem random to you, but it isnt To test, use the following code in a button click.. and note the first 5 messages shown...
VB Code:
Dim RandomNum As Single = (Int((9) * Rnd()))
MessageBox.Show(RandomNum.ToString)
Then close the application and run it again, doing the same... the same exact pattern of "random" digits will come up the second time as well...
6 4 5 2 2 on my system both times, because it is using the same seed value... thats why you use the Random class 
***Note - the code could be fixed by putting in a Randomize() statement, but thats still icky... use the Random class...
Last edited by gigemboy; Mar 31st, 2006 at 12:49 AM.
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
|