CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2013
    Posts
    3

    Question How to pick a random number from given set of numbers

    Hello everyone. I am in a last year of gymnasium and I really enjoy working with computers. Specialy while programming. I am a beginner (started few weeks ago) and I actually made my first mathematical program but with no random number generator, and just 10 expressions. After formatting my computer my first program was lost so I decided to make a new one with some improvments involved. With a help from a friend (which brought me into VB and tought me how VB actually works, but this is all I know) I made form for summating, form for subduction and form for multiplication. Now I am stuck at from for dividing.

    My idea is to make something like this:
    Program auto-generates number from 1-10 and puts it into label2. Then with a statments program randomly chooses one number from given set of numbers and puts it into label2.

    Example:
    //Program generates number 5, so label2.text = "5"
    If label2.text = "5" Then label1.text = [Solution] (So label1.text should be 5, 10, 15, 20, 25, 30, 35, 40, 45 or 50.)

    I already tried http://www.dreamincode.net/forums/to...set-of-number/ but this works just for 1 divisor (out of ten). So what to do? Oh, btw please explain your solution as you would explain it to the total beginner, someone who doesn't have a clue what VB is

    Thanks to all of you and have a nice day.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to pick a random number from given set of numbers

    A simple way to get a random number from a given group of numbers is to put those numbers into a list or array and then choose a random number from 0 to number of items in the list -1 which will give you a number that you can then use as an index to get the number from the list that is in that position. So in your example if the random number was 4 then it would grab the item at list index 4 which would be 25


    btw what does gymnasium have to do with computers or programming?
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: How to pick a random number from given set of numbers

    it is an advanced high school

  4. #4
    Join Date
    Jan 2013
    Posts
    3

    Re: How to pick a random number from given set of numbers

    Great, thanks, I think I get it now

    I mentioned Gymnasium to warn you, that I am not that good at programing

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to pick a random number from given set of numbers

    Gymnasium to me is the place where you play basketball
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    52

    Re: How to pick a random number from given set of numbers

    The Random class will do the trick: http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx

    call it's Next() method with a min and max, respectively being, 0, and the arrays Length for the exact number of elements in that array. This function would return the index, and you would use it to retrieve the element at that index being the "random" value chosen as DataMiser had mentioned.

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

Featured