Forum: Ruby 10 numbers in between 2 numbers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1df9fc8ddf084661265bbae74a8d0b43?d=identicon&s=25 jko170 (Guest)
on 2009-01-20 05:00
(Received via mailing list)
How can I get an array of 10 numbers in between two numbers?

(1..500).to_a

How can I turn the above into an array of ten numbers in between 1 and
500 (including 1 and 500)?
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-01-20 05:09
(Received via mailing list)
On Jan 19, 2009, at 9:58 PM, jko170 wrote:

> How can I get an array of 10 numbers in between two numbers?
>
> (1..500).to_a

I guess it depends on which ten numbers you want, but here's one way:

 >> (1..500).enum_for(:step, 50).to_a
=> [1, 51, 101, 151, 201, 251, 301, 351, 401, 451]

James Edward Gray II
3b1756d05466b4a78afd9aea7bb845c2?d=identicon&s=25 Aaron Turner (Guest)
on 2009-01-20 05:10
(Received via mailing list)
On Mon, Jan 19, 2009 at 7:58 PM, jko170 <jko170@gmail.com> wrote:
> How can I get an array of 10 numbers in between two numbers?
>
> (1..500).to_a
>
> How can I turn the above into an array of ten numbers in between 1 and
> 500 (including 1 and 500)?


I'm confused... aren't there a lot more then 10 numbers between 1 and
500?  Are you asking for a random sampling or???
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2009-01-20 08:15
(Received via mailing list)
jko170 wrote:

> How can I get an array of 10 numbers in between two numbers?
>
> (1..500).to_a
>
> How can I turn the above into an array of ten numbers in between 1 and
> 500 (including 1 and 500)?

10 numbers randomly, or spread out evenly (ish), such as 1, 50, 100,
150, 200, 250, 300, 350, 400, 450, 500 (though that's 11 and not
exactly even).  Can you elaborate?
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-01-20 14:41
(Received via mailing list)
On Jan 19, 2009, at 10:05 PM, James Gray wrote:

> On Jan 19, 2009, at 9:58 PM, jko170 wrote:
>
>> How can I get an array of 10 numbers in between two numbers?
>>
>> (1..500).to_a
>
> I guess it depends on which ten numbers you want, but here's one way:

Oops, I left out a:

require "enumerator"
1df9fc8ddf084661265bbae74a8d0b43?d=identicon&s=25 jko170 (Guest)
on 2009-01-20 16:42
(Received via mailing list)
On Jan 20, 1:12 am, Tim Greer <t...@burlyhost.com> wrote:
> exactly even).  Can you elaborate?
> --
> Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
> Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
> and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
> Industry's most experienced staff! -- Web Hosting With Muscle!

Sorry everyone, yes 10 numbers and dates spread our evenly. Thanks for
the replies!
1df9fc8ddf084661265bbae74a8d0b43?d=identicon&s=25 jko170 (Guest)
on 2009-01-20 16:45
(Received via mailing list)
On Jan 20, 1:12 am, Tim Greer <t...@burlyhost.com> wrote:
> exactly even).  Can you elaborate?
> --
> Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
> Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
> and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
> Industry's most experienced staff! -- Web Hosting With Muscle!

Sorry everyone, yes 10 numbers and dates spread our evenly. Thanks for
the replies!
1df9fc8ddf084661265bbae74a8d0b43?d=identicon&s=25 jko170 (Guest)
on 2009-01-20 23:47
(Received via mailing list)
On Jan 19, 10:05 pm, James Gray <ja...@grayproductions.net> wrote:
>
> James Edward Gray II

Yeah, the end numbers need to be included in the array of 10.
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-01-21 00:21
(Received via mailing list)
jko170 wrote:
>> James Edward Gray II
>
> Yeah, the end numbers need to be included in the array of 10.
>
>
Requirements: 10 numbers, evenly distributed, first and last numbers in
the array.

require 'pp'

first, last = ARGV[0].to_f, ARGV[1].to_f
numbers = []
incr = (last - first) / 9.0
n = first
10.times do
   numbers << n
   n += incr
end
pp numbers
1df9fc8ddf084661265bbae74a8d0b43?d=identicon&s=25 jko170 (Guest)
on 2009-01-21 19:26
(Received via mailing list)
On Jan 20, 5:19 pm, Tim Hunter <TimHun...@nc.rr.com> wrote:
>
> numbers = []
> incr = (last - first) / 9.0
> n = first
> 10.times do
>    numbers << n
>    n += incr
> end
> pp numbers
>
> --
> RMagick:http://rmagick.rubyforge.org/

Thank you very much Tim! Works perfectly.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-01-21 19:34
(Received via mailing list)
Hi --

On Thu, 22 Jan 2009, jko170 wrote:

>>>> => [1, 51, 101, 151, 201, 251, 301, 351, 401, 451]
>> first, last = ARGV[0].to_f, ARGV[1].to_f
>> RMagick:http://rmagick.rubyforge.org/
>
> Thank you very much Tim! Works perfectly.

Awww, you didn't say the final array didn't have to contain integers
:-)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
This topic is locked and can not be replied to.