Skip to content

Instantly share code, notes, and snippets.

@hierophantos
Created December 29, 2018 03:28
Show Gist options
  • Save hierophantos/0494cbbfcc6de4b1e1b22c42e207086d to your computer and use it in GitHub Desktop.
Save hierophantos/0494cbbfcc6de4b1e1b22c42e207086d to your computer and use it in GitHub Desktop.
(import (java.util.concurrent.ThreadLocalRandom))
(defn exp [x n]
(loop [acc 1 n n]
(if (zero? n) acc
(recur (* x acc) (dec n)))))
(defn random-bigint [limit]
(let [bits (.bitLength limit)]
(loop [result (BigInteger. bits (java.util.concurrent.ThreadLocalRandom/current))]
(if (< result limit)
(bigint result)
(recur (BigInteger. bits (java.util.concurrent.ThreadLocalRandom/current)))))))
(random-bigint (exp 2N 20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment