# Is this deterministic or not?

**URL:** <https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201>\
**Category:** Ruby\
**Created:** [June 1, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201 "2008-06-01T20:00:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![slix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@slix](https://www.ruby-forum.com/u/slix)\
**Post date:** [June 1, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201/1 "2008-06-01T20:00:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![slix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@slix](https://www.ruby-forum.com/u/slix)\
**Post date:** [June 1, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201/2 "2008-06-01T21:01:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![slix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@slix](https://www.ruby-forum.com/u/slix)\
**Post date:** [June 2, 2008, 11:38am UTC](https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201/3 "2008-06-02T11:38:22Z")

</div>

Tim H. 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.

---

<div class="post-metadata">

**Author:** ![slix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@slix](https://www.ruby-forum.com/u/slix)\
**Post date:** [June 24, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201/4 "2008-06-24T22:55:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![slix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@slix](https://www.ruby-forum.com/u/slix)\
**Post date:** [June 25, 2008, 11:45am UTC](https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201/5 "2008-06-25T11:45:52Z")

</div>

2008/6/2 Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Tim H. 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](http://www.ruby-doc.org/core/classes/Kernel.html#M001086)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![slix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@slix](https://www.ruby-forum.com/u/slix)\
**Post date:** [June 25, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201/6 "2008-06-25T23:01:01Z")

</div>

On 25.06.2008 19:34, Dave B. wrote:

> Robert K. 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

---

<div class="post-metadata">

**Author:** ![slix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@slix](https://www.ruby-forum.com/u/slix)\
**Post date:** [June 28, 2008, 5:55am UTC](https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201/7 "2008-06-28T05:55:44Z")

</div>

everyoen is missing my point. is the complete method deterministic?  
not the rand…

---

<div class="post-metadata">

**Author:** ![slix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@slix](https://www.ruby-forum.com/u/slix)\
**Post date:** [June 28, 2008, 2:05pm UTC](https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201/8 "2008-06-28T14:05:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![slix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@slix](https://www.ruby-forum.com/u/slix)\
**Post date:** [June 25, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/is-this-deterministic-or-not/139201/9 "2008-06-25T19:36:23Z")

</div>

Robert K. 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.”

> **[TrulyRandom - Perl interface to a truly random number generator function -...](https://metacpan.org/release/GARY/Math-TrulyRandom-1.0/view/TrulyRandom.pod)**
>
> Perl interface to a truly random number generator function
