Ok, perhaps I'm just dumb, but I have the problem with the following
code:
(import '(java.util ArrayList Collections))
(defn shuffle-java
"Shuffles coll using a Java ArrayList."
[coll]
(let [l (ArrayList. coll)]
(Collections/shuffle l)
(seq l)))
(defn- nodes-num [x-dim y-dim] (* x-dim y-dim))
(defn- states-num [x-dim y-dim] (* 4 x-dim y-dim))
(defn- particles-num [density nodes-num]
"Given a particle density between 0 and 1 and the number of nodes,
the funct. returns the rounded number of particles"
(int (* 4 density nodes-num)))
(defn- get-init-states [states-num particles-num]
"Returns the a shuffled sequence of states."
(doall (shuffle-java (concat (take particles-num (repeat 1))
(take (- states-num particles-num) (repeat
0))))))
(defn initialize-domain [x-dim y-dim density]
(let [nodes (nodes-num x-dim y-dim) states (states-num x-dim y-dim)
particles (particles-num density nodes)
seq-of-states (get-init-states states particles)]
(into {} (for [i (range x-dim) j (range y-dim)] {[i j] (vec (take
4 seq-of-states))}))))
(initialize-domain 4 4 0.3)
When you look at resulting hash, which you get by calling
"(initialize-domain 4 4 0.3)",
you see that the 4-component value vector of every key is "not so"
random. In fact it is the same vector every time.
But when I modify the "initialize-domain" function by deleting the
"seq-of-states" binding, and making a direct call to
the "get-init-states" function, the result is correct, i.e. for every
key in the resulting hash, the associated vector is random.
The "initialize-domain" function which yields the correct result is
this:
(defn initialize-domain [x-dim y-dim density]
(let [nodes (nodes-num x-dim y-dim) states (states-num x-dim y-dim)
particles (particles-num density nodes)]
(into {} (for [i (range x-dim) j (range y-dim)] {[i j] (vec (take
4 (get-init-states states particles)))}))))
I'm puzzled, please help.
Thank you, Oliver.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---