From: Josh Cheek Date: 2012-02-04T17:30:42+09:00 Subject: Re: 4 random numbers take highest 3 and add them together. --0016e6de1796510f3704b81f3e3f Content-Type: text/plain; charset=ISO-8859-1 On Fri, Feb 3, 2012 at 6:43 PM, h4y4shi 13bladex wrote: > I want to get 4 random numbers. The numbers will be between 1-6. I want > to disregard the smallest number and add the three biggest numbers > together. So far I wrote some ruby that I thought would do this but I > keep getting an error. > > statsrand = 0 > while statsrand == 0 > stats1a = (rand(6) + 1) > stats1b = (rand(6) + 1) > stats1c = (rand(6) + 1) > stats1d = (rand(6) + 1) > gstats1a = 0 > gstats1b = 0 > gstats1c = 0 > gstats1d = 0 > when (stats1a>stats1b) | (stats1a>stats1c) | (stats1a>stats1d) > gstats1a = stats1a > when (stats1b>stats1a) | (stats1b>stats1c) | (stats1b>stats1d) > gstats1b = stats1b > when (stats1c>stats1b) | (stats1c>stats1a) | (stats1c>stats1d) > gstats1c = stats1c > when (stats1d>stats1b) | (stats1d>stats1c) | (stats1d>stats1a) > gstats1d = stats1d > > mstr = gstats1a + gstats1b + gstats1c + gstats1d > > I cant find where the error is... > > -- > Posted via http://www.ruby-forum.com/. > > Hi, lots of problems here, to the point where it's very unclear where to even begin identifying them. I recommend you start with something simple that you can prove works to a given point (e.g. initializing the variables) then slowly build onto it (e.g. find the largest, second largest, third largest, and then their sum. This is very doable iteratively. In the end, I think Harry's solution is best (except both array indexes should count from the end so that it works for any given array of numbers). But since you're clearly struggling with something considerably simpler than that, it's probably best to see this idea through to the end in order to build up your knowledge and problem solving abilities. Then later, learn more about the fancy built in methods around enumerable classes. --0016e6de1796510f3704b81f3e3f--