Re: Trying to get a list of random numbers using repeat
You probably figured this out, but what you want is something like: (map rand-int (repeat SIZE MAX)) On Mar 23, 7:43 pm, Paul Drummond paul.drumm...@iode.co.uk wrote: Hi all, user= (repeat 10 (rand-int 49)) (4 4 4 4 4 4 4 4 4 4) Can someone please tell me why this doesn't work? It must be something obvious but I can't see the wood for the trees right now - it's late and my head hurts! Thanks, Paul. -- Iode Software Ltd, registered in England No. 6299803. Registered Office Address: 12 Sancroft Drive, Houghton-le-Spring, Tyne Wear, DH5 8NE. This message is intended only for the use of the person(s) (the intended recipient(s)) to whom it is addressed. It may contain information which is privileged and confidential within the meaning of applicable law. If you are not the intended recipient, please contact the sender as soon as possible. The views expressed in this communication may not necessarily be the views held by The Company. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Trying to get a list of random numbers using repeat
2009/3/26 Jon jon.na...@gmail.com: You probably figured this out, but what you want is something like: (map rand-int (repeat SIZE MAX)) In fact - I didn't think of this - thanks! This is what I used: (map rand-int (repeat Integer/MAX_VALUE)) I have been around since the beginning of Clojure but my journey to learning it is taking forever! I will get there one day! If only I could use it at work :) Cheers, Paul. -- Iode Software Ltd, registered in England No. 6299803. Registered Office Address: 12 Sancroft Drive, Houghton-le-Spring, Tyne Wear, DH5 8NE. This message is intended only for the use of the person(s) (the intended recipient(s)) to whom it is addressed. It may contain information which is privileged and confidential within the meaning of applicable law. If you are not the intended recipient, please contact the sender as soon as possible. The views expressed in this communication may not necessarily be the views held by The Company. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Trying to get a list of random numbers using repeat
On Mar 24, 12:43 pm, Paul Drummond paul.drumm...@iode.co.uk wrote: Hi all, user= (repeat 10 (rand-int 49)) (4 4 4 4 4 4 4 4 4 4) Can someone please tell me why this doesn't work? I think this explains your problem: http://xkcd.com/221/ ;-) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Trying to get a list of random numbers using repeat
2009/3/25 David Plumpton david.plump...@gmail.com: I think this explains your problem: http://xkcd.com/221/ There is something to be said for that function. At least it has no side-effects ;) -- Iode Software Ltd, registered in England No. 6299803. Registered Office Address: 12 Sancroft Drive, Houghton-le-Spring, Tyne Wear, DH5 8NE. This message is intended only for the use of the person(s) (the intended recipient(s)) to whom it is addressed. It may contain information which is privileged and confidential within the meaning of applicable law. If you are not the intended recipient, please contact the sender as soon as possible. The views expressed in this communication may not necessarily be the views held by The Company. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Trying to get a list of random numbers using repeat
2009/3/23 Krešimir Šojat kso...@gmail.com: (rand-int 49) will produce one integer, and repeat will repeat it 10 times, that is why you see same number repeated. How embarrassing! As soon as I switched my machine off last night I realised rand-int was only being called once - of course it was! I did consider 'repeatedly' but I couldn't get it to work for some reason - thank you for showing me how to use it. Also, I was put off from using it when I read its doc-string which says: Takes a function of no args, presumably with side effects, and returns an infinite lazy sequence of calls to it Why presumably with side effects? Cheers, Paul. -- Iode Software Ltd, registered in England No. 6299803. Registered Office Address: 12 Sancroft Drive, Houghton-le-Spring, Tyne Wear, DH5 8NE. This message is intended only for the use of the person(s) (the intended recipient(s)) to whom it is addressed. It may contain information which is privileged and confidential within the meaning of applicable law. If you are not the intended recipient, please contact the sender as soon as possible. The views expressed in this communication may not necessarily be the views held by The Company. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Trying to get a list of random numbers using repeat
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. Joshua On Tue, Mar 24, 2009 at 10:04 AM, Paul Drummond paul.drumm...@iode.co.ukwrote: 2009/3/23 Krešimir Šojat kso...@gmail.com: (rand-int 49) will produce one integer, and repeat will repeat it 10 times, that is why you see same number repeated. How embarrassing! As soon as I switched my machine off last night I realised rand-int was only being called once - of course it was! I did consider 'repeatedly' but I couldn't get it to work for some reason - thank you for showing me how to use it. Also, I was put off from using it when I read its doc-string which says: Takes a function of no args, presumably with side effects, and returns an infinite lazy sequence of calls to it Why presumably with side effects? Cheers, Paul. -- Iode Software Ltd, registered in England No. 6299803. Registered Office Address: 12 Sancroft Drive, Houghton-le-Spring, Tyne Wear, DH5 8NE. This message is intended only for the use of the person(s) (the intended recipient(s)) to whom it is addressed. It may contain information which is privileged and confidential within the meaning of applicable law. If you are not the intended recipient, please contact the sender as soon as possible. The views expressed in this communication may not necessarily be the views held by The Company. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Trying to get a list of random numbers using repeat
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! Thanks to both of you for helping me see sense! Cheers, Paul. -- Iode Software Ltd, registered in England No. 6299803. Registered Office Address: 12 Sancroft Drive, Houghton-le-Spring, Tyne Wear, DH5 8NE. This message is intended only for the use of the person(s) (the intended recipient(s)) to whom it is addressed. It may contain information which is privileged and confidential within the meaning of applicable law. If you are not the intended recipient, please contact the sender as soon as possible. The views expressed in this communication may not necessarily be the views held by The Company. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Trying to get a list of random numbers using repeat
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 iThe * Art of Computer Programming,/i Volume 2: iSeminumerical * 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 -~--~~~~--~~--~--~---
Re: Trying to get a list of random numbers using repeat
user= (repeat 10 (rand-int 49)) (4 4 4 4 4 4 4 4 4 4) Hi, (rand-int 49) will produce one integer, and repeat will repeat it 10 times, that is why you see same number repeated. To fix this use: user= (take 10 (repeatedly #(rand-int 49))) (21 29 9 20 15 34 8 28 16 26) -- Krešimir Šojat --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---