Results 1 to 7 of 7

Thread: easy question

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    easy question

    what command do i use to make a random number 1-1000 pop up into a label?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member OMITT3D's Avatar
    Join Date
    Mar 2006
    Posts
    368

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: easy question

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member OMITT3D's Avatar
    Join Date
    Mar 2006
    Posts
    368

    Re: easy question

    I am using that in an app atm and it works fine

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: easy question

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: easy question

    Quote 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:
    1. Dim RandomNum As Single = (Int((9) * Rnd()))
    2. 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
  •  



Click Here to Expand Forum to Full Width