
January 20th, 2013, 02:37 PM
#1
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 autogenerates number from 110 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...setofnumber/ 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.

January 22nd, 2013, 03:29 AM
#2
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.

January 22nd, 2013, 11:17 AM
#3
Re: How to pick a random number from given set of numbers
it is an advanced high school

January 26th, 2013, 08:52 AM
#4
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

January 26th, 2013, 09:53 AM
#5
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.

February 3rd, 2013, 03:40 PM
#6
Re: How to pick a random number from given set of numbers
The Random class will do the trick: http://msdn.microsoft.com/enus/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

Forum Rules

Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.
Featured
