4 random numbers take highest 3 and add them together

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…

h4y4shi 13bladex [email protected] wrote:

stats1d = (rand(6) + 1)
when (stats1d>stats1b) | (stats1d>stats1c) | (stats1d>stats1a)
gstats1d = stats1d

mstr = gstats1a + gstats1b + gstats1c + gstats1d

I cant find where the error is…

It would be easier for others to help you if you included the actual
error message you get in addition to the code that purportedly produced
the error. Not everyone seeing your post has the ability (that’s me at
the moment) or desire to copy and paste your code, but they may be able
to help anyway with the error message in hand. :slight_smile:

-Jeremy

On Sat, Feb 4, 2012 at 9:43 AM, h4y4shi 13bladex
[email protected] 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.

This is a different approach but if it doesn’t work, I think it is
easier to fix errors if you have them because it is short and easier
to read.

Once you have your random numbers try something like this.

arr = [2,4,1,5]
p arr.sort[1…-1].inject{|a,b| a+b}

Hope it helps.

Harry

On Sat, Feb 4, 2012 at 9:43 AM, h4y4shi 13bladex
[email protected] 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.

If you don’t like inject, this is even shorter and easier to understand.
It’s not very flexible but…
If you always have 4 numbers and always want to add the largest 3
numbers,

r = [2,4,1,5]
p r[0]+r[1]+r[2]+r[3]-r.min

Harry

Hi, h4y4shi

I think that error from some causes.

  • while # not closed(and not need)
  • when # when only in case
  • | # points different meanings, use “||” or “or” for this case

below codes tested in 1.9.3p0

ex)

stats1a = (rand(6) + 1)
stats1b = (rand(6) + 1)
stats1c = (rand(6) + 1)
stats1d = (rand(6) + 1)
gstats1a = 0
gstats1b = 0
gstats1c = 0
gstats1d = 0

if (stats1a>stats1b) || (stats1a>stats1c) || (stats1a>stats1d)
gstats1a = stats1a
end

if (stats1b>stats1a) || (stats1b>stats1c) || (stats1b>stats1d)
gstats1b = stats1b
end

if (stats1c>stats1b) || (stats1c>stats1a) || (stats1c>stats1d)
gstats1c = stats1c
end

if (stats1d>stats1b) || (stats1d>stats1c) || (stats1d>stats1a)
gstats1d = stats1d
end

mstr = gstats1a + gstats1b + gstats1c + gstats1d

ex2)

another approach

stats = 4.times.map{rand(6) + 1}
min = stats.min
p stats.reject{|n|n == min}.inject(:+)

2012/2/4 h4y4shi 13bladex [email protected]:

sorry, my ex2 is not safety(if [1, 1, 3, 6])
I reccomend Harry’s approaching.

fixed-ex2)

stats = 4.times.map{rand(6) + 1}
min = stats.min
p (stats.inject(:+) - stats.min)

On Fri, Feb 3, 2012 at 6:43 PM, h4y4shi 13bladex
<[email protected]

wrote:

stats1d = (rand(6) + 1)
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.

Thank you Harry. Thank you everyone. This works.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs