Search code examples
clojureclojure-contrib

Generating pseudorandom numbers drawn from a uniform distribution


I'm looking to build a function in Clojure that outputs m x n matrix of pseudorandom numbers drawn from the open interval (0,1). The specified inputs would be the row dimension m and column dimension n. I have familiarity with constructed matrices, nested vectors, in Clojure but have never generated random (psuedo) numbers before. My first guess, if starting from scratch, would use modular arithmetic to pump out inputs for the m x n matrix. Is this the easiest way to implement this idea?


Solution

  • The built-in (rand) generates a number from the uniform between 0 and 1. So with:

    (for [row (range m)]
      (for [column (range n)]
        (rand)))
    

    there is no need to implement your own generator.