like this: (defn shuffle-java ;;from Shawn Hoover post on these google groups "Shuffles coll using a Java ArrayList. Blows shuffle out of the water on speed and space." [coll] (let [l (java.util.ArrayList. coll)] (java.util.Collections/shuffle l) (seq l)))
(defn random-number-list [max dim] (take dim (shuffle-java (range 0 max)))) user> (random-number-list 100 20) (38 70 73 57 10 81 32 99 92 19 77 39 27 24 47 17 86 8 58 76) On Aug 25, 1:30 pm, Jonathan Smith <jonathansmith...@gmail.com> wrote: > Why not make a list of numbers from 0 to max, randomly shuffle it, > and do a take on the resulting sequence until you have enough numbers. > > On Aug 25, 10:16 am, sebastien <sebastien....@gmail.com> wrote: > > > What is the most efficient way to generate list of unique random > > numbers? The function must look something like this: > > > (defn make-random-numbers [dim max] > > ....) > > > where dim is dimensionality of the vector, and max is the upper bound > > of random numbers. > > The problem is not to allow duplicates in the sequence. I've found two > > options to solve this task: > > 1) the most straightforward way is to check if sequence already > > contains new element on every addition, but it requires O(n!) steps > > for a list of n numbers; > > 2) using clojure sets will make it easy to add unique numbers, but it > > will return sorted sequence, so i need to shuffle it by myself, and i > > don't see efficient way to do this. > > > So, is there any other ways to do the task? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---