Quoting Heinz Strunk <lists@ruby-forum.com>:
> Hello,
>
> I'm working with probability and Ruby doesn't really does as I want.
> Just to make it easier to understand I'll focus on the problem.
>
> I have some event that should occur in 20% of all events. I thought I'd
> make it easy by just using rand like following:
> num = 20 - rand(100)
> if(num > 0)
> do_stuff
> end
>
> But "do_stuff" is by far not being called as often as I would like it
> to. Is there any method or something that is more accurate with that
> kind of stuff?
>
The code looks correct, though a bit hard to understand. If it were my code,
I might refactor it like this:
do_stuff if rand(100) < 20
I dumped the first line into irb and tried it. It looked like do_stuff
wouldn't be called often enough, so I actually counted the occurrences and
crunched the numbers. They were within a reasonable distance of 1 in 5. I
suggest actually running the code and counting the occurrences. I suspect
you'll find that 20% is less often then it seems.
HTH,
Jeffrey
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment