10 numbers in between 2 numbers

J

jko170

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)?
 
J

James Gray

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, 51, 101, 151, 201, 251, 301, 351, 401, 451]

James Edward Gray II
 
A

Aaron Turner

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???


--
Aaron Turner
http://synfin.net/
http://tcpreplay.synfin.net/ - Pcap editing and replay tools for Unix & Windows
They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety. -- Benjamin Franklin
 
T

Tim Greer

jko170 said:
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?
 
J

jko170

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?
--
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!
 
J

jko170

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?
--
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!
 
T

Tim Hunter

jko170 said:
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

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
 
J

jko170

jko170 said:
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
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

Thank you very much Tim! Works perfectly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top