Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

A Random Post About Random Numbers In CojureScript

3/6/2018

0 Comments

 
Picture

Dip A Toe In Some ClojureScript

If you are new to ClojureScript I highly recommend copying and pasting the code snippets below into app.klise.tech, my personal favorite online REPL for evaluating ClojureScript code.

JavaScript's Math.random()

Since ClojureScript has great JS interop, we can call our good old "Math.random()" function from JavaScript. In clojure syntax we put the function first prefixed with a dot ( . ), followed by object / class containing the function, in this case the Math class of our super interop js construct.
(.random js/Math)
Since the JavaScript Math class in converted to the ClojureScript Math namespace you can even just write this:
(Math/random)
Is this not the most beautifully succinct line of code you've ever seen? "Evaluate the random function in the Math namespace". Muah. It's a slap in the face to OOP and brings a tear to the eye of bruffles the unicorn who lives on the planet neptune. Remember that we are in JavaScript land now, and this still gives you a random JS floating point number between 0 (inclusive) and 1 (exclusive).

Clojure's Rand

My second way of creating random numbers in clojurescript is to use the built-in clojure function, rand.
(rand)
Rand has been around since back in the good ole' days when there was only JVM clojure. An interesting fact is that ClojureScript's rand is actually implemented using the same Math.random() as our first method. Rand is a thin abstraction on top of Math.random, and a cool little thing to note about it is that you can pass in a number n to get a number between 0 (inclusive) and the number n (exclusive) which you can't do with JavaScript's Math.random. I normally use Webstorm with the Cursive plugin, and I was actually able to just type "(rand)" into a cljs file, ctrl + click the word rand, then jump directly to the implementation for rand in clojurescript's core library. Try doing that in emacs (and definitely leave a comment if it actually is possible). Just in case you have a lame editor where you can't ctrl + click right to the file and line, you can check out the file and line I'm talking about here. It's really not complicated at all and is based upon this simple calculation: 
(* (Math/random) n)
Imo just "(math/random)" it much shorter, cleaner, and just makes me feel good when I read and write it.

Rand-Int

Don't you just love function names that are so obvious you almost don't even need to look at the documentation to make sure what it does? Well, rand-int just returns a random int, and to quote the doc string exactly it, "Returns a random integer between 0 (inclusive) and 1 (exclusive)." This one basically just takes the random float that we would make between 0 and n as passes it into "Math/floor". You can see the true implementation in all its breathtaking awe and beauty here.
(rand-int 5)

Rand-Nth

I'll be honest, I found out about rand-nth from the first time I ctrl+ clicked rand and started snooping around clojurescript's core.cljs file, but I'm glad I did because it's one cool ass motherf%ckin' function! Basically, it's a rand function for collections that will return a random element from the collection.
(rand-nth ["a" "b" "c"])

(rand-nth '("a" "b" "c"))

Why Random Numbers Are Beautiful

A random number is like a blooming cherry blossom that must be nurtured and appreciated. If you have three objects in front of you, how do you truly randomly choose one? There's something about as humans that we cannot by our own brains generate totally random numbers. If you needed a random number on the spot what would you do? You might pull out your cell phone and Google for "random number generator", and that's exactly my point. The ability to create this unbiased variety of selection is an incredible power wielded by few other than the computer programmer. Take it, my friends, and do great things with it!
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021