On Sep 19, 2011, at 1:03 PM, Chuck Remes wrote:

>
> On Sep 19, 2011, at 11:52 AM, ֧ ֧ wrote:
>
>> Hi,
>>
>> How to generate rand (random number) between two numbers?
>> I mean not between 0 and some number. It should be between some  
>> number (not 0) and some number.
>
> Try this:
>
> irb(main):001:0> def random_number(from, to)
> irb(main):002:1> from + rand((to - from).abs)
> irb(main):003:1> end
> => #<Rubinius::CompiledMethod random_number file=(irb)>
> irb(main):004:0> random_number(6, 9)
> => 6
> irb(main):005:0> random_number(6, 9)
> => 7
> irb(main):006:0> random_number(6, 9)
> => 6
> irb(main):007:0> random_number(6, 9)
> => 6
>
>
> cr

Or if you don't mind opening a core class:
irb1.9.2> class Range
irb1.9.2>   def sample
irb1.9.2>     span = self.end - self.begin
irb1.9.2>     span += 1 unless self.exclude_end?
irb1.9.2>     self.begin + rand(span.to_i)
irb1.9.2>     end
irb1.9.2>   end
#1.9.2 => nil
irb1.9.2> (6..9).sample
#1.9.2 => 7
irb1.9.2> (6..9).sample
#1.9.2 => 8
irb1.9.2> (6..9).sample
#1.9.2 => 7
irb1.9.2> (6..9).sample
#1.9.2 => 9
irb1.9.2> (6..9).sample
#1.9.2 => 8
irb1.9.2> (6..9).sample
#1.9.2 => 7
irb1.9.2> (6..9).sample
#1.9.2 => 6



Rob Biedenharn		
Rob / AgileConsultingLLC.com	http://AgileConsultingLLC.com/
rab / GaslightSoftware.com		http://GaslightSoftware.com/