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
-Rob
Rob Biedenharn
R...@AgileConsultingLLC.com http://AgileConsultingLLC.com/
r...@GaslightSoftware.com http://GaslightSoftware.com/
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
Although I prefer Rob's suggestion as a better example of self
documented code; this is another alternative...
def usually_one
[1,2,2,2,2][rand(5)]
end
...there's many ways to skin a cat :-)
*ahem* of course.. this is the total opposite of what the OP asked
for, and returns 1 in 20% of the calls :-D
sorry for not paying attention.... oops!
Ha! Vindicated
ruby-1.8.7-p302 > begin
ruby-1.8.7-p302 > t=Time.now
ruby-1.8.7-p302 ?> 1000000.times{(rand(100) + 120)/100}
ruby-1.8.7-p302 ?> puts Time.now-t
ruby-1.8.7-p302 ?> end
0.905431
ruby-1.8.7-p302 > begin
ruby-1.8.7-p302 > t=Time.now
ruby-1.8.7-p302 ?> 1000000.times{rand < 0.80 ? 1 : 2}
ruby-1.8.7-p302 ?> puts Time.now-t
ruby-1.8.7-p302 ?> end
1.712769
ruby-1.8.7-p302 > begin
ruby-1.8.7-p302 > t=Time.now
ruby-1.8.7-p302 ?> 1000000.times{ [1,2,2,2,2][rand(5)] }
ruby-1.8.7-p302 ?> puts Time.now-t
ruby-1.8.7-p302 ?> end
1.150481
> ...there's many ways to skin a cat :-)
But how fast can you do it?
Colin
I expected to get different times, but to keep the proportions the
same... but I get a little difference in order of speed:
>> begin
?> t=Time.now
>> 1000000.times{(rand(100) + 120)/100}
>> puts Time.now-t
>> end
1.200484
>> begin
?> t=Time.now
>> 1000000.times{rand < 0.80 ? 1 : 2}
>> puts Time.now-t
>> end
1.249109
>> begin
?> t=Time.now
>> 1000000.times{[1,2,2,2,2][rand(5)]}
>> puts Time.now-t
>> end
1.498434
>> ...there's many ways to skin a cat :-)
>
> But how fast can you do it?
A fair bit faster if I don't build the array in every loop ;-)
>> x=[1,2,2,2,2]
=> [1, 2, 2, 2, 2]
>> begin
?> t=Time.now
>> 1000000.times{x[rand(5)]}
>> puts Time.now-t
>> end
1.026202
OK, I admit defeat. I am using ruby 1.8.7 which may be the reason for
the different ratios you are seeing (assuming you are using 1.9.2).
Interestingly the benefit of your technique is even greater on 1.8.7,
taking only 0.67 secs on my machine.
Colin
Nah! Who cares if it takes half-a-second or a second to select a
million random numbers.... any of the methods are perfect for
selecting one number.
Was a nice distraction from the recent run of posts...
The OP might be doing some statistical simulation, so might need
millions of values, but probably not.
Colin
>
> Was a nice distraction from the recent run of posts...
>
> --
> 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 rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
But how fast can you do it?
> ...there's many ways to skin a cat :-)
Colin
I think I need a new laptop.
Colin
Nice, simple process (assuming it works... I've not tried it), but I
don't think it has much to do with the topic of this thread, and it's
not likely to create a *real* UUID (RFC4122) is it?
This old thread discusses it a bit:
http://www.ruby-forum.com/topic/164078
Ooh, that's an evil tweak... (a minor diversion which is much more fun
than what I was doing)
Lenovo T61p, Core2Duo, T9300, 2.5GHz, ruby 1.9.2-p0
= 0.253025
And for curiosity, my Ubuntu 10.10 VM on top of the same hardware:
= 0.323791
--
Posted via http://www.ruby-forum.com/.