user=> (take 100 (distinct (repeatedly #(rand-int 200)))) (100 55 65 188 90 150 144 72 137 74 187 158 163 28 140 146 111 116 135 88 29 81 36 173 149 79 16 105 82 162 60 20 49 50 91 176 165 3 56 22 9 85 44 101 33 134 186 128 141 103 92 143 123 23 129 83 80 5 172 179 166 167 66 195 99 164 38 138 148 130 6 155 32 175 120 18 197 190 78 19 46 7 77 68 76 189 63 27 4 43 24 183 122 51 95 157 153 156 199 110)
Adjust this for whatever random-generator you want to use. On Feb 18, 3:31 pm, Vitaly Peressada <vit...@ufairsoft.com> wrote: > I want to get first N (for example 100) unique random floats. Here is > what I have. > > (defn rand-flt [max-flt] > (format "%.2f" (rand max-flt))) > > (defn gen-rand-flts [max-flt] > (lazy-seq (cons (rand-flt max-flt) (gen-rand-flts max-flt)))) > > (defn get-n-floats [max-float, how-many] > (let [tf-fun (fn [] (take how-many (gen-rand-flts max-float)))] > (loop [sof (set (tf-fun))] > (if (> (count sof) (dec how-many)) > (take how-many sof) > (recur (set (concat sof (tf-fun)))))))) > > (get-n-floats 100.0 10) > > This works but I feel that should be more idiomatic solution for get-n- > floats. I have tried take-while, some, even read about delay/force but > couldn't come up with a shorter alternative; main problem is realizing > lazy seq. Any suggestions? -- 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