Skip to content

Commit

Permalink
Merge pull request trystan#1 from Sandarr95/master
Browse files Browse the repository at this point in the history
Adding shuffle to the random-seed library
  • Loading branch information
trystan authored Sep 16, 2020
2 parents e4daf82 + 916b392 commit 0acf96c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ Supply your own random seed to the clojure.core random functions.

## Leiningen

[random-seed "1.0.0"]
[random-seed "1.0.1"]

## Usage

(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 0acf96c

Please sign in to comment.