On Tuesday 23 December 2008 14:44, Chris Bunch wrote:
> I've got the newest version of Clojure and wrote a simple program to
> generate a list of 100 random numbers, but I seem to get a lot of
> duplicate numbers (especially when I pick a large upper bound for
> rand-int). My code is pretty similar to an example in the Programming
> Clojure book:
>
> (defn gen-rands [max-val]
>   (loop [result [] x 100]
>     (if (zero? x)
>       (sort result)
>       (recur (conj result (rand-int max-val)) (dec x))
>     )
>   )
> )
>
> ...
>
> Is there anything I've done wrong that would result in me getting
> 2147483647 so many times? Thanks in advance!

Another fun fact: The values in the returned sequence are monotonically 
increasing, at least as long as you don't try to create values beyond 
Integer/MAX_VALUE:

user=> (apply < (gen-rands Integer/MAX_VALUE))
true

user=> (apply < (gen-rands Integer/MAX_VALUE))
true

user=> (apply < (gen-rands Integer/MAX_VALUE))
true

I tried 20 times with the same result each time. If you increase the 
range much beyond MAX_VALUE, you start to get the repeats and then the 
monotonically increasing property ceases to hold, though monotonically 
non-decreasing does hold apparently without limit.


Randall Schulz

--~--~---------~--~----~------------~-------~--~----~
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