10 numbers in between 2 numbers

Discussion in 'Ruby' started by jko170, Jan 20, 2009.

  1. jko170

    jko170 Guest

    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)?
     
    jko170, Jan 20, 2009
    #1
    1. Advertising

  2. jko170

    James Gray Guest

    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
     
    James Gray, Jan 20, 2009
    #2
    1. Advertising

  3. jko170

    Aaron Turner Guest

    On Mon, Jan 19, 2009 at 7:58 PM, 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)?



    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
     
    Aaron Turner, Jan 20, 2009
    #3
  4. jko170

    Tim Greer Guest

    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?
    --
    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!
     
    Tim Greer, Jan 20, 2009
    #4
  5. jko170

    James Gray Guest

    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"

    > >> (1..500).enum_for:)step, 50).to_a

    > => [1, 51, 101, 151, 201, 251, 301, 351, 401, 451]
    >
    > James Edward Gray II
    >
     
    James Gray, Jan 20, 2009
    #5
  6. jko170

    jko170 Guest

    On Jan 20, 1:12 am, Tim Greer <> wrote:
    > 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?
    > --
    > 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!
     
    jko170, Jan 20, 2009
    #6
  7. jko170

    jko170 Guest

    On Jan 20, 1:12 am, Tim Greer <> wrote:
    > 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?
    > --
    > 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!
     
    jko170, Jan 20, 2009
    #7
  8. jko170

    jko170 Guest

    On Jan 19, 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:
    >
    > >> (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.
     
    jko170, Jan 20, 2009
    #8
  9. jko170

    Tim Hunter Guest

    jko170 wrote:
    > On Jan 19, 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:
    >>
    >> >> (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

    --
    RMagick: http://rmagick.rubyforge.org/
     
    Tim Hunter, Jan 20, 2009
    #9
  10. jko170

    jko170 Guest

    On Jan 20, 5:19 pm, Tim Hunter <> wrote:
    > jko170 wrote:
    > > On Jan 19, 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:

    >
    > >>  >> (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
    >
    > --
    > RMagick:http://rmagick.rubyforge.org/


    Thank you very much Tim! Works perfectly.
     
    jko170, Jan 21, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Subra
    Replies:
    25
    Views:
    1,257
    user923005
    Mar 8, 2007
  2. Andrew Tatum

    Fibonacci Numbers and Lucas Numbers

    Andrew Tatum, May 26, 2007, in forum: C++
    Replies:
    6
    Views:
    608
    Howard
    May 27, 2007
  3. Lance Hoffmeyer
    Replies:
    2
    Views:
    550
    Lance Hoffmeyer
    Jul 26, 2007
  4. GIMME
    Replies:
    5
    Views:
    200
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
  5. D'Anne Asquith

    Numbers not acting like numbers

    D'Anne Asquith, Oct 1, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    109
    Heinrich Mislik
    Oct 2, 2003
Loading...

Share This Page