On Mar 23, 2009, at 10:36 PM, Paul Drummond wrote:

>
> 2009/3/24 Joshua Fox <joshuat...@gmail.com>:
>>> Why "presumably with side effects?"
>> Otherwise you would use repeat. A pure  function returns the same  
>> value
>> every time, so there is no reason to call it repeatedly.
>
> Yup, that makes sense.
>
> Random numbers are side-effecting (is that the right term?) and I was
> trying to think about the (simple) problem in a pure functional way
> which was getting my simple brain all confused!
>

It's not obvious what side effect is occurring by calling rand-int,  
but in fact the state of the pseudorandom generator is affected by  
each call. This is from java.util.Random. Everybody calls next()  
sooner or later, and it calls the compareAndSet() method:

    * This is a linear congruential pseudorandom number generator, as
      * defined by D. H. Lehmer and described by Donald E. Knuth in  
<i>The
      * Art of Computer Programming,</i> Volume 2: <i>Seminumerical
      * Algorithms</i>, section 3.2.1.
    protected int next(int bits) {
         long oldseed, nextseed;
         AtomicLong seed = this.seed;
         do {
         oldseed = seed.get();
         nextseed = (oldseed * multiplier + addend) & mask;
         } while (!seed.compareAndSet(oldseed, nextseed));
         return (int)(nextseed >>> (48 - bits));
     }

As Joshua pointed out, whenever a function returns different values  
given the same args that's a good sign that a side effect has  
occurred--some state has obviously changed.

Aloha,
David Sletten


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to