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? Thanks in advance! -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
The code snippet seems OK to me, although the code you have in the if block will happen 80% not 20% of the time, and it could be made a bit "slimmer". But you say it happens less than 20%? Tried this on by Ubuntu box with Ruby 1.8.7: irb(main):021:0> counter = 0 => 0 irb(main):022:0> 1000000.times{counter += 1 if rand(100) < 20} => 1000000 irb(main):023:0> puts String(Float(counter)/10000) + "%" 20.0002% => nil So, given a million iterations, it seems to work OK, and gives the expected result of close to 20% hitrate. But if you still have problems, then I''m at a loss.... Some platform specific seeding issue perhaps? Well, hope this helps... :o) Regards, Rolf On Sun, Mar 21, 2010 at 5:12 PM, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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? > > Thanks in advance! > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Quoting Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>:> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Quoting Jeffrey L. Taylor <ror-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org>: [snip]> 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. >Oops, change to "less often than it seems." 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
The Sun, 21 Mar 2010 17:12:02 +0100, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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?As said in another answer, this is convoluted code, simply testing rand(100) < 20 is ok. To answer your original question, rand implementations don''t do what most developers think they do : they are pseudo-random number generators and the randomness is not guaranteed for only one call to rand in a whole program. If you try to get a random value only once in your program, you should feed the number generator a truly random value (for example read 4 bytes from /dev/urandom on a modern Unix box and feed them as an Integer to the generator through Kernel.srand). Lionel -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks guys, I found the problem. There was another factor that made look the rand not correct. Everything''s just fine now :) -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.