# Optimization

**URL:** <https://www.ruby-forum.com/t/optimization/170654>\
**Category:** Ruby\
**Created:** [June 20, 2009, 11:39am UTC](https://www.ruby-forum.com/t/optimization/170654 "2009-06-20T11:39:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [June 20, 2009, 11:39am UTC](https://www.ruby-forum.com/t/optimization/170654/1 "2009-06-20T11:39:27Z")

</div>

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?
