what command do i use to make a random number 1-1000 pop up into a label? :mad: :mad:
Printable View
what command do i use to make a random number 1-1000 pop up into a label? :mad: :mad:
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.
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.Quote:
Originally Posted by OMITT3D
I am using that in an app atm and it works fine :ehh:
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.Quote:
Originally Posted by OMITT3D
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...Quote:
Originally Posted by OMITT3D
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...VB Code:
Dim RandomNum As Single = (Int((9) * Rnd())) MessageBox.Show(RandomNum.ToString)
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...