Is this deterministic or not?

Discussion in 'Ruby' started by slix, Jun 1, 2008.

  1. slix

    slix Guest

    puts "Enter a sentence: "
    puts gets.split.sort_by { rand }.join(" ")

    takes a sentence and scrambles the words. how is this implemented? i
    tried myself first with a for or while loop and tried if word is not
    in new_sentence then new_sentence += word etc.
    but that could go on for a long time and would be very slow for a
    large text.
    slix, Jun 1, 2008
    #1
    1. Advertising

  2. slix

    Tim Hunter Guest

    slix wrote:
    > puts "Enter a sentence: "
    > puts gets.split.sort_by { rand }.join(" ")
    >
    > takes a sentence and scrambles the words. how is this implemented? i
    > tried myself first with a for or while loop and tried if word is not
    > in new_sentence then new_sentence += word etc.
    > but that could go on for a long time and would be very slow for a
    > large text.
    >
    >


    I assume you already know how gets, split, and join work and the
    question in your subject refers to sort_by. The sort_by method sorts the
    enumerable (in this case, the array of words produced by split) using
    the keys generated by the block. That is, sort_by calls the block once
    for each element in the array. The block usually (but not in this case)
    inspects the element and returns a key. You're wanting a random sort, so
    you use the rand method to return a random number as the key. The
    sort_by method sorts the array in increasing order by the random
    numbers. Since the keys are randomly generated, the array is randomly
    ordered.

    Is it deterministic? See my P.S. You can use srand to ensure that rand
    returns the same sequence of numbers.

    The doc for sort_by is pretty good. Try "ri sort_by", and "ri Kernel#rand".

    P.S. I'm sure there's a lot of good things to be said about PRNGs and
    stuff but I'll let somebody else type it in.

    --
    RMagick: http://rmagick.rubyforge.org/
    RMagick 2: http://rmagick.rubyforge.org/rmagick2.html
    Tim Hunter, Jun 1, 2008
    #2
    1. Advertising

  3. slix

    Dave Bass Guest

    Tim Hunter wrote:
    > P.S. I'm sure there's a lot of good things to be said about PRNGs and
    > stuff but I'll let somebody else type it in.


    I'll just add that Ruby's rand is deterministic, but it uses a very good
    PRNG algorithm (Mersenne twister). Unless you really need true
    randomness, it's good enough to use out-of-the-box.
    --
    Posted via http://www.ruby-forum.com/.
    Dave Bass, Jun 2, 2008
    #3
  4. slix

    cirfu Guest

    but is the function he is talking about deterministic? i mean rand
    could be but he could theoretically guess the same word over and over.

    os will this function-execution-time grow according to a formula or it
    depends on "how lucky" he is?
    cirfu, Jun 24, 2008
    #4
  5. 2008/6/2 Dave Bass <>:
    > Tim Hunter wrote:
    >> P.S. I'm sure there's a lot of good things to be said about PRNGs and
    >> stuff but I'll let somebody else type it in.

    >
    > I'll just add that Ruby's rand is deterministic, but it uses a very good
    > PRNG algorithm (Mersenne twister). Unless you really need true
    > randomness, it's good enough to use out-of-the-box.


    Plus, opposed to other libs and languages, there is a random element in seeding:

    http://www.ruby-doc.org/core/classes/Kernel.html#M001086

    Kind regards

    robert


    --
    use.inject do |as, often| as.you_can - without end
    Robert Klemme, Jun 25, 2008
    #5
  6. slix

    Dave Bass Guest

    Robert Klemme wrote:
    > Plus, opposed to other libs and languages, there is a random element in
    > seeding


    Well, a combination of the time, the process id, and a sequence number
    isn't actually random! ;-)

    However, it's certainly better than using the time on its own, as has
    commonly been done in the past.

    Perl has a Math::TrulyRandom module. "The source of the randomness is
    from interrupt timing discrepancies."

    http://search.cpan.org/~gary/Math-TrulyRandom-1.0/TrulyRandom.pod
    --
    Posted via http://www.ruby-forum.com/.
    Dave Bass, Jun 25, 2008
    #6
  7. On 25.06.2008 19:34, Dave Bass wrote:
    > Robert Klemme wrote:
    >> Plus, opposed to other libs and languages, there is a random element in
    >> seeding

    >
    > Well, a combination of the time, the process id, and a sequence number
    > isn't actually random! ;-)
    >
    > However, it's certainly better than using the time on its own, as has
    > commonly been done in the past.


    Which in turn is even better than using a constant seed - which also has
    been done in the past. This was the case I was thinking of.

    Cheers

    robert
    Robert Klemme, Jun 25, 2008
    #7
  8. slix

    defn noob Guest

    everyoen is missing my point. is the complete method deterministic?
    not the rand...
    defn noob, Jun 28, 2008
    #8
  9. On 28.06.2008 05:51, defn noob wrote:
    > everyoen is missing my point. is the complete method deterministic?
    > not the rand...


    What complete method? You presented two lines of code and in those the
    only potential source of indeterminism is #rand (apart from the user
    input), which is precisely why the discussion revolves around this.

    Cheers

    robert
    Robert Klemme, Jun 28, 2008
    #9
    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. Replies:
    1
    Views:
    397
    Richard Tobin
    Nov 4, 2003
  2. Replies:
    1
    Views:
    362
    Patrick TJ McPhee
    Nov 5, 2003
  3. Gijs
    Replies:
    0
    Views:
    402
  4. Philippe Poulard
    Replies:
    0
    Views:
    413
    Philippe Poulard
    Sep 7, 2004
  5. Sahatra Kumara

    Non-deterministic schema

    Sahatra Kumara, Mar 24, 2005, in forum: XML
    Replies:
    0
    Views:
    521
    Sahatra Kumara
    Mar 24, 2005
Loading...

Share This Page