Sunil S Nandihalli <sunil.nandiha...@gmail.com> writes:

Hi Sunil,

> yea true, the full sequence has to be realized before it is shuffled
> .. but however the thing is that I am only interested in the first
> couple of elements of the shuffled sequence.

Well, in that case, you can use the standard shuffle, right?  For
example, to get a random permutation of the first 10 natural numbers,
you can do:

  (shuffle (take 10 (iterate inc 0)))
  ==> [1 6 9 8 4 5 3 7 0 2]

However, you cannot get a seq of 10 randomly chosen natural numbers,
because that would mean to realize the seq of natural numbers.

  (take 10 (shuffle (iterate inc 0)))
  ==> Runs till hell freezes over

But if you specify an upper limit, then it'll work again.

  (take 10 (shuffle (take 1000 (iterate inc 0))))
  ==> (748 460 353 349 586 743 994 404 468 306)

Of course, that will realize the first 1000 natural numbers, although
you only take 10 out of it.

Well, in that use-case, you'd probably want to go with rand-int anyway.

Bye,
Tassilo

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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