On 12 March 2011 13:42, Rob Biedenharn <[email protected]> wrote:
>
> On Mar 12, 2011, at 5:37 AM, Colin Law wrote:
>
>> On 12 March 2011 10:10, Erwin <[email protected]> wrote:
>>>
>>> I know how to generate a random number between 1 and 2 :
>>>
>>> def random
>>>  return (1 + rand(2))

No it doesn't actually.  1 + rand  would be better, though it depends
what you mean by *between* whether even that is correct.

>>> end
>>>
>>> but let's say  I would like to return 1 in 80% of the calls and 2 in
>>> 20% of the call...
>>> which library should I use ?  ( I've seen a lot at
>>> http://raa.ruby-lang.org/
>>> )
>>>
>>
>> No doubt there are better ways but something like
>> (rand(100) + 120)/100
>> should do it.
>>
>> Colin
>
>
> Well, perhaps something that reveals the intention better:
>
> def usually_one
>  rand < 0.80 ? 1 : 2
> end

Yes, much better.  I suppose it is the old programmer in me
instinctively avoiding floating point to save on processor time.  A
bit silly when everything has hardware floating point.

Colin

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to