Skip to content

Commit

Permalink
Added shuffle function using rng from set-random-seed\!, updated read…
Browse files Browse the repository at this point in the history
…me and project.clj, updated and tested clojure 1.8.0
  • Loading branch information
Sandarr95 committed Dec 9, 2016
1 parent e4daf82 commit 1a5c48a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Supply your own random seed to the clojure.core random functions.

(ns example.core
(:require [random-seed.core :refer :all])
(:refer-clojure :exclude [rand rand-int rand-nth]))
(:refer-clojure :exclude [rand rand-int rand-nth shuffle]))

(set-random-seed! 888)

Expand All @@ -20,6 +20,8 @@ Supply your own random seed to the clojure.core random functions.

(rand-nth [:a :b :c])

(shuffle [:a :b :c])

## License

Copyright © 2015 Trystan
Expand Down
6 changes: 3 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(defproject random-seed "1.0.0"
:description "Copies of rand, rand-int, and rand-nth, except you can specify the seed."
(defproject random-seed "1.0.1"
:description "Copies of rand, rand-int, rand-nth and shuffle, except you can specify the seed."
:url "https://www.github.com/trystan/random-seed"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]])
:dependencies [[org.clojure/clojure "1.8.0"]])
10 changes: 9 additions & 1 deletion src/random_seed/core.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns random-seed.core
(:refer-clojure :exclude [rand rand-int rand-nth]))
(:refer-clojure :exclude [rand rand-int rand-nth shuffle]))


(defonce rng (new java.util.Random))
Expand Down Expand Up @@ -30,3 +30,11 @@
specified in set-random-seed!."
[coll]
(nth coll (rand-int (count coll))))

(defn shuffle
"Return a random permutation of coll. Works like clojure.core/shuffle
except it uses the seed specified in set-random-seed!."
[^java.util.Collection coll]
(let [al (java.util.ArrayList. coll)]
(java.util.Collections/shuffle al rng)
(clojure.lang.RT/vector (.toArray al))))

0 comments on commit 1a5c48a

Please sign in to comment.