Regarding rand

Discussion in 'Ruby' started by Ben Stephens, Jul 19, 2005.

  1. Ben Stephens

    Ben Stephens Guest

    I was reading Learn to Program by Chris Pine yesterday, in one bit it says:
    =09' Note that I used rand(101) to get back numbers from 0 to 100, and
    that rand(1) always gives back 0.'

    I was wondering if rand() could support syntax like:
    rand(0..100)

    Here's my go:

    def random(value)
    =09if value.class.method_defined? 'entries'
    =09=09entries =3D value.entries
    =09=09entries[rand(entries.length)]
    =09else
    =09=09rand(value)
    =09end
    end

    this works with arrays and hashes too.

    I'm coming to Ruby via RoR and i'm loving it, I should be writing a
    web app and i'm playing with Ruby instead.

    Ben
    Ben Stephens, Jul 19, 2005
    #1
    1. Advertising

  2. Ben Stephens <> writes:

    > I was wondering if rand() could support syntax like:
    >
    > rand(0..100)


    That's a great idea!

    > Here's my go:
    >
    > def random(value)
    > if value.class.method_defined? 'entries'
    > entries = value.entries
    > entries[rand(entries.length)]
    > else
    > rand(value)
    > end
    > end
    >
    > this works with arrays and hashes too.


    Oh, I don't know about that... I'd do something like this:

    def random(range)
    case range
    when Integer
    rand(range)
    when Range
    a, b = [range.begin, range.end].sort
    if a.integer? and b.integer? then
    if range.exclude_end?
    then rand(b - a) + a
    else rand(b - a + 1) + a end
    else
    rand * (b - a) + a
    end
    end
    end

    > I'm coming to Ruby via RoR and i'm loving it, I should be
    > writing a web app and i'm playing with Ruby instead.


    That's the downside of using such a powerful language:
    You can never stop playing with it! :)

    --
    Daniel Brockman <>

    So really, we all have to ask ourselves:
    Am I waiting for RMS to do this? --TTN.
    Daniel Brockman, Jul 19, 2005
    #2
    1. Advertising

  3. Ben Stephens

    Jacob Fugal Guest

    On 7/19/05, Ben Stephens <> wrote:
    > I was reading Learn to Program by Chris Pine yesterday, in one bit it say=

    s:
    > ' Note that I used rand(101) to get back numbers from 0 to 100, =

    and
    > that rand(1) always gives back 0.'
    >=20
    > I was wondering if rand() could support syntax like:
    > rand(0..100)


    An extension I've frequently used is this:

    module Enumerable
    def random
    ary =3D self.to_a
    ary[rand(ary.length)]
    end
    end

    This is purposefully naive. It won't work with enumerables that don't
    terminate (e.g. a sequence generator), but then again, neither will
    several other methods from Enumerable, such as select, reject, etc. It
    will work, but is inefficient for large non-array Enumerables such as
    the range (0..1_000_000) since it has to convert it to a 1_000_000
    member array. However, in the most frequent case of a relatively small
    array of range, it can be quite useful:

    # random number between 1 and 100
    number =3D (1..100).random

    # random letter from A to Z
    letter =3D ('A'..'Z').random

    # random primary color
    color =3D [ :red, :green, :blue ].random

    The first two are much more efficient if I calculate the random
    number, but I think this unification of random selection from an
    Enumerable is useful. I might possibly write an optimized version for
    Range eventually...

    Jacob Fugal
    Jacob Fugal, Jul 19, 2005
    #3
    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. Niko D. Barli

    rand function in Modelsim 5.7c

    Niko D. Barli, Aug 26, 2004, in forum: VHDL
    Replies:
    9
    Views:
    6,296
    Niko D. Barli
    Sep 6, 2004
  2. daniel kaplan

    rand() question

    daniel kaplan, Sep 15, 2004, in forum: Perl
    Replies:
    4
    Views:
    620
    Ian Sedwell
    Sep 21, 2004
  3. Amelyan

    what is rand?

    Amelyan, Mar 31, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    534
    Kevin Spencer
    Mar 31, 2006
  4. Orhan Demirel

    usage of rand()

    Orhan Demirel, Jul 21, 2003, in forum: C++
    Replies:
    1
    Views:
    390
    Adam Fineman
    Jul 21, 2003
  5. 7stud --

    rand() v. rand(0.1) ?

    7stud --, Sep 15, 2007, in forum: Ruby
    Replies:
    6
    Views:
    219
    Morton Goldberg
    Sep 16, 2007
Loading...

Share This Page