Hi Chris, unfortunately, I don't remember enough math right now to be able
to offer a better solution than what you've already come up with.
The only catch that I can think of is that since you are looking for a
*percentage*, the "many" in your "many.times do" loop needs to be a
relatively large number for it to produce the kinds of numbers you want.
That is, "many" should be >= 1000. If it's less than that, your random
number generator may have too much variability and your methods won't be
called the desired percentages. (I confirmed this for myself by writing a
small script based on your pseudo code below.)
If "many" is a smaller number, you may need to come up with another approach
to ensure the desired percentages. (e.g. I can think of another approach
using array lengths... bit more complex than what you've got now.)
If "many" is a random or unknown value, talk to a mathematician! ;-)
Let me know how it goes. I'm interested to hear how this turns out for you.
Cheers. Paul C.
On 12/02/07, Chris McMahon <[EMAIL PROTECTED]> wrote:
Say I have 3 methods a, b, c. Over the course of many iterations, I
want to call each method a certain percentage of the time. Say I
have a config file like
method percentage
a 30
b 30
c 40
I'm thinking of writing code that will load up integer ranges like
arange = [1..30]
brange = [31..60]
crange = [61..100]
and then do
many.times do |foo|
num= rand(100)
if num in arange
a
end
if num in brange
b
end
if num in crange
end
end #loop
So two questons:
First, is this is a sane approach? Is there any more elegant way to
call an arbitrary method a certain percentage of the time for a random
number of iterations?
_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general