From: Robert Feldt Date: 2013-03-07T21:01:51+09:00 Subject: Re: Random#rand_inclusive? --f46d0444036ca9184804d7547872 Content-Type: text/plain; charset=ISO-8859-1 Thanks Matthew, didn't know that about 2.0. Cheers, Robert On Thu, Mar 7, 2013 at 12:21 PM, Matthew Kerwin wrote: > On 7 March 2013 17:55, Robert Feldt wrote: > >> >> Random#rand and Kernel#rand are exclusive i.e. they return a value >= 0.0 >> but > the max? >> > > I'm back at a computer now, and have looked up the docs for Random#rand. > Apparently in 2.0 you can simply pass an inclusive range and it will do > what you want, e.g.: Random.rand(0.0..max) < > http://ruby-doc.org/core-2.0/Random.html#method-i-rand> > > However in 1.9, yeah, your rand_incl2 is basically what I'd do. I > independently came up with a simplified version: > https://gist.github.com/phluid61/5107356 > -- Best regards, /Robert Feldt -- Tech. Dr. (PhD), Assoc. Professor in Software Engineering Chalmers, Software Engineering Dept Blekinge Institute of Technology, Software Engineering Research Lab robert.feldt (a) chalmers.se or robert.feldt (a) gmail.com Mobile phone: +46 (0) 733 580 580 http://www.cse.chalmers.se/~feldt --f46d0444036ca9184804d7547872 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thanks Matthew, didn't know that about 2.0.

Cheers,<= /div>

Robert

On Thu, M= ar 7, 2013 at 12:21 PM, Matthew Kerwin <matthew@kerwin.net.au><= /span> wrote:
=
On 7 March 2013 17:55, Robert = Feldt <robert.feldt@gmail.com> wrote:

Rand= om#rand and Kernel#rand are exclusive i.e. they return a value >=3D 0.0 = but <max if max is a Float. What if one wants a rand that is inclusive o= f the max?

I'm back at a computer now, and = have looked up the docs for Random#rand.=A0 Apparently in 2.0 you can simpl= y pass an inclusive range and it will do what you want, e.g.:=A0 Random.ran= d(0.0..max)=A0 <http://ruby-doc.org/core-2.0/Random.html#metho= d-i-rand>

However in 1.9, yeah, your rand_incl2 is basically what I= 9;d do.=A0 I independently came up with a simplified version: https://gist.gith= ub.com/phluid61/5107356



--
Best regards= ,

/Robert Feldt
--
Tech. Dr. (PhD), Assoc. Professor in Softwa= re Engineering
Chalmers, Software Engineering Dept
Blekinge Institute= of Technology, Software Engineering Research Lab
robert.feldt (a) chalmers.= se=A0 =A0=A0 or=A0 =A0=A0 robert.feldt (a) gmail.com
Mobile phone: +46 (0) 733 580 580
http://www.cs= e.chalmers.se/~feldt
--f46d0444036ca9184804d7547872--