Optimization

C

Ch Ba

I could use some help in optimizing a rather simple method. All it does
is randomly select a few instance variables and increase them while
decreasing another. The problem is that whether or not that particular
variable can be increased depends on if it's value is less than that of
another variable. This is what I was thinking.

[untested code]
class Foo
def initialize(x)
@a = 0
@b = 0
@c = 0
@d = 0
@x = x
bar()
end

def bar
values = [@a,@b,@c,@d]
while @x > values.sort.first
selection = values[rand(values.size)]
if selection <= @x
@x-=selection;selection+=1
else
values.delete selection
end
end
end

end
[/untested code]
Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,054
Members
48,769
Latest member
Clifft

Latest Threads

Top