how to create random object to a particular ruby object ?

Discussion in 'Ruby' started by Pokkai Dokkai, Apr 11, 2008.

  1. how to create random object to a particular ruby object ?

    for example i want like this

    rand(Fixnum) --> 345 (randomly)
    rand(Float) --> 3877.5545(randomly)
    rand(String) --> "sfskgksf" (randomly)
    rand(boolean) --> 0(randomly)

    any idea ?
    --
    Posted via http://www.ruby-forum.com/.
     
    Pokkai Dokkai, Apr 11, 2008
    #1
    1. Advertising

  2. Re: how to create random object to a particular ruby object ?

    2008/4/11, Pokkai Dokkai <>:
    > how to create random object to a particular ruby object ?
    >
    > for example i want like this


    You need more input parameters:

    > rand(Fixnum) --> 345 (randomly)

    Number range?

    > rand(Float) --> 3877.5545(randomly)

    Number range?

    > rand(String) --> "sfskgksf" (randomly)

    Length of String? Chars allowed in String?

    > rand(boolean) --> 0(randomly)

    That's easy
    rand(2) == 0

    Cheers

    robert


    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Apr 11, 2008
    #2
    1. Advertising

  3. Pokkai Dokkai

    Robert Dober Guest

    Re: how to create random object to a particular ruby object ?

    On Fri, Apr 11, 2008 at 1:16 PM, Robert Klemme
    <> wrote:
    > 2008/4/11, Pokkai Dokkai <>:
    >
    > > how to create random object to a particular ruby object ?
    > >
    > > for example i want like this

    >
    > You need more input parameters:
    >
    > > rand(Fixnum) --> 345 (randomly)

    > Number range?
    >
    >
    > > rand(Float) --> 3877.5545(randomly)

    > Number range?
    >
    > > rand(String) --> "sfskgksf" (randomly)

    > Length of String? Chars allowed in String?
    >
    > > rand(boolean) --> 0(randomly)

    > That's easy
    > rand(2) == 0

    I am surprised that you are so permissive Robert ;).
    I would say

    [true,false][rand(2)]

    or even, just to have some fun

    [false,nil][rand(2)].send([:&&,:||][rand(2)],
    method_returning_a_completeley_random_object)

    to reflect Ruby's what's "true" and what's "false" semantics.

    Cheers
    Robert
    > --
    > use.inject do |as, often| as.you_can - without end


    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Apr 11, 2008
    #3
  4. Pokkai Dokkai

    Robert Dober Guest

    Re: how to create random object to a particular ruby object ?

    > [false,nil][rand(2)].send([:&&,:||][rand(2)],
    > method_returning_a_completeley_random_object)

    oops, whats going wrong in my brain I was 100% sure that && was a
    method, but :&& is not even a symbol, what is the reason for that?
    I am obviously missing the obvious....
    R.
     
    Robert Dober, Apr 11, 2008
    #4
  5. Re: how to create random object to a particular ruby object ?

    Hi --

    On Fri, 11 Apr 2008, Robert Dober wrote:

    >> [false,nil][rand(2)].send([:&&,:||][rand(2)],
    >> method_returning_a_completeley_random_object)

    > oops, whats going wrong in my brain I was 100% sure that && was a
    > method, but :&& is not even a symbol, what is the reason for that?
    > I am obviously missing the obvious....


    I'm not sure what the exact rule is, but for operators I think you
    always have to quote them to get their symbol:

    :"&&"


    David

    --
    Rails training from David A. Black and Ruby Power and Light:
    ADVANCING WITH RAILS April 14-17 New York City
    INTRO TO RAILS June 9-12 Berlin
    ADVANCING WITH RAILS June 16-19 Berlin
    See http://www.rubypal.com for details and updates!
     
    David A. Black, Apr 11, 2008
    #5
  6. Pokkai Dokkai

    Robert Dober Guest

    Re: how to create random object to a particular ruby object ?

    On Fri, Apr 11, 2008 at 2:39 PM, David A. Black <> wrote:
    > Hi --
    >
    >
    >
    > On Fri, 11 Apr 2008, Robert Dober wrote:
    >
    >
    > >
    > > > [false,nil][rand(2)].send([:&&,:||][rand(2)],
    > > > method_returning_a_completeley_random_object)
    > > >

    > > oops, whats going wrong in my brain I was 100% sure that && was a
    > > method, but :&& is not even a symbol, what is the reason for that?
    > > I am obviously missing the obvious....
    > >

    >
    > I'm not sure what the exact rule is, but for operators I think you
    > always have to quote them to get their symbol:
    >
    > :"&&"

    No David
    irb(main):017:0* x=:&
    irb(main):018:0* 15.send x, 8
    => 8
    irb(main):019:0>

    The simple "problem" is that I believed for 3 years that &&, ||, :and
    and :eek:r where methods (of Object), which they are not :(
    irb(main):020:0* 15.send("&&", 42)
    NoMethodError: undefined method `&&' for 15:Fixnum
    from (irb):20:in `send'
    from (irb):20
    from :0

    and unless somebody can point out a good reason why that is like that
    I am really tempted to make a RCR for 1.9.
    Opinions?

    Thx in advance
    Robert

    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Apr 11, 2008
    #6
  7. Re: how to create random object to a particular ruby object ?

    On Fri, Apr 11, 2008 at 3:16 PM, Robert Dober <> wrote:
    > On Fri, Apr 11, 2008 at 2:39 PM, David A. Black <> wrote:
    > > Hi --
    > >
    > >
    > >
    > > On Fri, 11 Apr 2008, Robert Dober wrote:
    > >
    > >
    > > >
    > > > > [false,nil][rand(2)].send([:&&,:||][rand(2)],
    > > > > method_returning_a_completeley_random_object)
    > > > >
    > > > oops, whats going wrong in my brain I was 100% sure that && was a
    > > > method, but :&& is not even a symbol, what is the reason for that?
    > > > I am obviously missing the obvious....
    > > >

    > >
    > > I'm not sure what the exact rule is, but for operators I think you
    > > always have to quote them to get their symbol:
    > >
    > > :"&&"

    > No David
    > irb(main):017:0* x=:&
    > irb(main):018:0* 15.send x, 8
    > => 8
    > irb(main):019:0>
    >
    > The simple "problem" is that I believed for 3 years that &&, ||, :and
    > and :eek:r where methods (of Object), which they are not :(
    > irb(main):020:0* 15.send("&&", 42)
    > NoMethodError: undefined method `&&' for 15:Fixnum
    > from (irb):20:in `send'
    > from (irb):20
    > from :0
    >
    > and unless somebody can point out a good reason why that is like that
    > I am really tempted to make a RCR for 1.9.
    > Opinions?
    >
    > Thx in advance
    > Robert


    Because the use of these operators is that in some cases you don't
    want the right hand to evaluate.
    result = long_operation or other_long_operation

    stops evaluating after the first one returns non-nil/false

    ^ manveru

    >
    >
    >
    > --
    > http://ruby-smalltalk.blogspot.com/
    >
    > ---
    > Whereof one cannot speak, thereof one must be silent.
    > Ludwig Wittgenstein
    >
    >
     
    Michael Fellinger, Apr 11, 2008
    #7
  8. Pokkai Dokkai

    Robert Dober Guest

    Re: how to create random object to a particular ruby object ?

    On Fri, Apr 11, 2008 at 3:28 PM, Michael Fellinger
    <> wrote:
    >
    > > > > I am obviously missing the obvious....

    > Because the use of these operators is that in some cases you don't
    > want the right hand to evaluate.
    > result = long_operation or other_long_operation
    >
    > stops evaluating after the first one returns non-nil/false
    >

    Missing the obvious, I was right after all, thank you Michael.

    Robert


    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Apr 11, 2008
    #8
  9. Re: how to create random object to a particular ruby object ?

    Hi --

    On Fri, 11 Apr 2008, Robert Dober wrote:

    > On Fri, Apr 11, 2008 at 2:39 PM, David A. Black <> wrote:
    >> Hi --
    >>
    >>
    >>
    >> On Fri, 11 Apr 2008, Robert Dober wrote:
    >>
    >>
    >>>
    >>>> [false,nil][rand(2)].send([:&&,:||][rand(2)],
    >>>> method_returning_a_completeley_random_object)
    >>>>
    >>> oops, whats going wrong in my brain I was 100% sure that && was a
    >>> method, but :&& is not even a symbol, what is the reason for that?
    >>> I am obviously missing the obvious....
    >>>

    >>
    >> I'm not sure what the exact rule is, but for operators I think you
    >> always have to quote them to get their symbol:
    >>
    >> :"&&"

    > No David
    > irb(main):017:0* x=:&


    & isn't an operator, though; it's a method. I don't know whether
    that's actually why there's the difference with regard to symbol-izing
    them, but I think it does at least mostly fall along those lines.


    David

    --
    Rails training from David A. Black and Ruby Power and Light:
    ADVANCING WITH RAILS April 14-17 New York City
    INTRO TO RAILS June 9-12 Berlin
    ADVANCING WITH RAILS June 16-19 Berlin
    See http://www.rubypal.com for details and updates!
     
    David A. Black, Apr 11, 2008
    #9
  10. Re: how to create random object to a particular ruby object ?

    2008/4/11, Robert Dober <>:
    > On Fri, Apr 11, 2008 at 1:16 PM, Robert Klemme
    > <> wrote:
    > > 2008/4/11, Pokkai Dokkai <>:
    > >
    > > > how to create random object to a particular ruby object ?
    > > >
    > > > for example i want like this

    > >
    > > You need more input parameters:
    > >
    > > > rand(Fixnum) --> 345 (randomly)

    > > Number range?
    > >
    > >
    > > > rand(Float) --> 3877.5545(randomly)

    > > Number range?
    > >
    > > > rand(String) --> "sfskgksf" (randomly)

    > > Length of String? Chars allowed in String?
    > >
    > > > rand(boolean) --> 0(randomly)

    > > That's easy
    > > rand(2) == 0

    >
    > I am surprised that you are so permissive Robert ;).


    I'm not getting the joke here, sorry. Is it somehow related to the
    original posting mentioning "0" as the sole return value from
    rand(boolean)?

    Cheers

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Apr 11, 2008
    #10
  11. Pokkai Dokkai

    Robert Dober Guest

    Re: how to create random object to a particular ruby object ?

    On Fri, Apr 11, 2008 at 4:41 PM, Robert Klemme
    <> wrote:
    > 2008/4/11, Robert Dober <>:


    > I'm not getting the joke here, sorry. Is it somehow related to the
    > original posting mentioning "0" as the sole return value from
    > rand(boolean)?

    well I would not consider 0 a boolean, but maybe that was what OP wanted.

    R.
     
    Robert Dober, Apr 11, 2008
    #11
  12. Pokkai Dokkai

    Robert Dober Guest

    Re: how to create random object to a particular ruby object ?

    On Fri, Apr 11, 2008 at 4:25 PM, David A. Black <> wrote:

    >
    > & isn't an operator, though; it's a method. I don't know whether
    > that's actually why there's the difference with regard to symbol-izing
    > them, but I think it does at least mostly fall along those lines.

    I guess that this is the reason of which I fail to see the reason, but
    that is not an important issue to me.
    For me it was important to got explained why && was not a method,
    which has been nicely accomplished in the meantime.

    Cheers
    Robert
     
    Robert Dober, Apr 11, 2008
    #12
  13. Re: how to create random object to a particular ruby object ?

    2008/4/11, Robert Dober <>:
    > On Fri, Apr 11, 2008 at 4:41 PM, Robert Klemme
    > <> wrote:
    > > 2008/4/11, Robert Dober <>:

    >
    >
    > > I'm not getting the joke here, sorry. Is it somehow related to the
    > > original posting mentioning "0" as the sole return value from
    > > rand(boolean)?

    >
    > well I would not consider 0 a boolean,


    And a random sequence of 0's would look funny. :)

    > but maybe that was what OP wanted.


    In which case I would have missed the mark because my bit does not
    return 0 but true / false. :)

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Apr 11, 2008
    #13
  14. Pokkai Dokkai

    Robert Dober Guest

    Re: how to create random object to a particular ruby object ?

    On Fri, Apr 11, 2008 at 5:24 PM, Robert Klemme
    <> wrote:
    > 2008/4/11, Robert Dober <>:
    > > On Fri, Apr 11, 2008 at 4:41 PM, Robert Klemme
    > > <> wrote:
    > > > 2008/4/11, Robert Dober <>:

    > >
    > >
    > > > I'm not getting the joke here, sorry. Is it somehow related to the
    > > > original posting mentioning "0" as the sole return value from
    > > > rand(boolean)?

    > >
    > > well I would not consider 0 a boolean,

    >
    > And a random sequence of 0's would look funny. :)
    >
    >
    > > but maybe that was what OP wanted.

    >
    > In which case I would have missed the mark because my bit does not
    > return 0 but true / false. :)

    It does indeed, I am in great form today, but at least you understand
    my surprise now, that you would deliver rand(2) instead of rand(2) ==
    0, which indeed is a nice way to do the job!
    Sorry
    R.
    >
    > Kind regards
    >
    >
    >
    > robert
    >
    > --
    > use.inject do |as, often| as.you_can - without end
    >
    >




    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Apr 11, 2008
    #14
  15. Re: how to create random object to a particular ruby object ?

    On Fri, Apr 11, 2008 at 2:17 AM, Pokkai Dokkai <> wrote:
    > how to create random object to a particular ruby object ?
    >
    > for example i want like this
    >
    > rand(Fixnum) --> 345 (randomly)
    > rand(Float) --> 3877.5545(randomly)
    > rand(String) --> "sfskgksf" (randomly)
    > rand(boolean) --> 0(randomly)


    The cleanest way is to add the class method make_random to all your
    classes. For example

    class String
    def self.make_random(length = nil)
    length ||= rand(15) # well, we have to pick *something*
    (0...length).map { (rand(96) + 32).chr } .join("")
    end
    end

    irb(main):006:0> String.make_random
    => "Y3+W%2("
    irb(main):007:0> String.make_random(6)
    => "I9K@Nr"
    irb(main):008:0> String.make_random(10)

    martin
     
    Martin DeMello, Apr 11, 2008
    #15
  16. Re: how to create random object to a particular ruby object ?

    Martin DeMello wrote:
    > On Fri, Apr 11, 2008 at 2:17 AM, Pokkai Dokkai <> wrote:
    >> how to create random object to a particular ruby object ?
    >>
    >> for example i want like this
    >>
    >> rand(Fixnum) --> 345 (randomly)
    >> rand(Float) --> 3877.5545(randomly)
    >> rand(String) --> "sfskgksf" (randomly)
    >> rand(boolean) --> 0(randomly)

    >
    > The cleanest way is to add the class method make_random to all your
    > classes. For example


    Here is the dirtiest, wrongest, ugliest way to do it:

    module ObjectSpace
    def self.random_object(klass)
    n = each_object(klass){}
    i = rand(n)
    each_object klass do |obj|
    if i == 0
    return obj
    else
    i -= 1
    end
    end
    return nil
    end
    end

    p ObjectSpace.random_object(String)
    p ObjectSpace.random_object(Float)

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Apr 12, 2008
    #16
  17. Re: how to create random object to a particular ruby object ?

    On 12.04.2008 07:50, Joel VanderWerf wrote:
    > Martin DeMello wrote:
    >> On Fri, Apr 11, 2008 at 2:17 AM, Pokkai Dokkai <> wrote:
    >>> how to create random object to a particular ruby object ?
    >>>
    >>> for example i want like this
    >>>
    >>> rand(Fixnum) --> 345 (randomly)
    >>> rand(Float) --> 3877.5545(randomly)
    >>> rand(String) --> "sfskgksf" (randomly)
    >>> rand(boolean) --> 0(randomly)

    >> The cleanest way is to add the class method make_random to all your
    >> classes. For example

    >
    > Here is the dirtiest, wrongest, ugliest way to do it:
    >
    > module ObjectSpace
    > def self.random_object(klass)
    > n = each_object(klass){}
    > i = rand(n)
    > each_object klass do |obj|
    > if i == 0
    > return obj
    > else
    > i -= 1
    > end
    > end
    > return nil
    > end
    > end
    >
    > p ObjectSpace.random_object(String)
    > p ObjectSpace.random_object(Float)


    This does not work well for Strings because you get only instances that
    do exist already when the method is called. That way you a) have no
    clear definition of what you get (length, contained chars...) and b) you
    either need to dup or freeze the return value. Both have their
    problems: #freeze has side effects and #dup does not work for all classes.

    Kind regards

    robert
     
    Robert Klemme, Apr 12, 2008
    #17
  18. Pokkai Dokkai

    Marc Heiler Guest

    Re: how to create random object to a particular ruby object ?

    > #freeze has side effects
    Actually I think I would not say "side effects", I would rather claim
    that if one does .freeze an object, this object becomes rather unusable
    (as you can not normally undo a .freeze again), I actually cannot recall
    that I really needed .freeze in any code i wrote
    --
    Posted via http://www.ruby-forum.com/.
     
    Marc Heiler, Apr 12, 2008
    #18
  19. Re: how to create random object to a particular ruby object ?

    On 12.04.2008 15:33, Marc Heiler wrote:
    >> #freeze has side effects

    > Actually I think I would not say "side effects",


    Even if you would not use that term it is the proper CS term for what's
    happening here. :)

    > I would rather claim
    > that if one does .freeze an object, this object becomes rather unusable


    Usability is changed. Whether the object is usable or unusable depends
    on what the code wants to do with it.

    > (as you can not normally undo a .freeze again), I actually cannot recall
    > that I really needed .freeze in any code i wrote


    It comes in handy when you want to prevent a particular class of errors,
    i.e. all those that are caused by unintentionally changing an object.
    Once you find that a constant is not as constant as you thought you
    might start using #freeze.

    Kind regards

    robert
     
    Robert Klemme, Apr 12, 2008
    #19
  20. Re: how to create random object to a particular ruby object ?

    Robert Klemme wrote:
    > On 12.04.2008 07:50, Joel VanderWerf wrote:
    >> Martin DeMello wrote:
    >>> On Fri, Apr 11, 2008 at 2:17 AM, Pokkai Dokkai
    >>> <> wrote:
    >>>> how to create random object to a particular ruby object ?
    >>>>
    >>>> for example i want like this
    >>>>
    >>>> rand(Fixnum) --> 345 (randomly)
    >>>> rand(Float) --> 3877.5545(randomly)
    >>>> rand(String) --> "sfskgksf" (randomly)
    >>>> rand(boolean) --> 0(randomly)
    >>> The cleanest way is to add the class method make_random to all your
    >>> classes. For example

    >>
    >> Here is the dirtiest, wrongest, ugliest way to do it:
    >>
    >> module ObjectSpace
    >> def self.random_object(klass)
    >> n = each_object(klass){}
    >> i = rand(n)
    >> each_object klass do |obj|
    >> if i == 0
    >> return obj
    >> else
    >> i -= 1
    >> end
    >> end
    >> return nil
    >> end
    >> end
    >>
    >> p ObjectSpace.random_object(String)
    >> p ObjectSpace.random_object(Float)

    >
    > This does not work well for Strings because you get only instances that
    > do exist already when the method is called. That way you a) have no
    > clear definition of what you get (length, contained chars...) and b) you
    > either need to dup or freeze the return value. Both have their
    > problems: #freeze has side effects and #dup does not work for all classes.


    I did say "wrongest" ;)

    For floats, you usually get e, pi, or the max/min floats, if no other
    floats have been instantiated.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Apr 12, 2008
    #20
    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. Darren Clark

    Random NOt random?

    Darren Clark, Jun 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    491
    mikeb
    Jun 24, 2004
  2. Akaketwa
    Replies:
    1
    Views:
    5,068
    impaler
    Sep 22, 2006
  3. globalrev
    Replies:
    4
    Views:
    818
    Gabriel Genellina
    Apr 20, 2008
  4. VK
    Replies:
    15
    Views:
    1,332
    Dr J R Stockton
    May 2, 2010
  5. Replies:
    10
    Views:
    153
Loading...

Share This Page