liveBook · Manning

Rand

As explained in Clojure: The Essential Reference

rand is a Clojure function that returns a pseudorandom floating-point number generated using the Java runtime's random-number facilities.

rand returns a pseudorandom double. With no arguments, its result is at least 0.0 and less than 1.0, making it useful for probability checks and lightweight randomized behavior. section 5.10

Key properties:

  • Accepts no arguments for the standard form; the listed additional arity accepts a double bound. section 5.10
  • Returns a double rather than an integer. section 5.10
  • Provides pseudorandom values through Java’s random-number facilities, which are suitable for many practical uses but not stronger randomness requirements. section 5.10

A/B choice

A probability check can select one of two outcomes: (defn A-B [prob A B] (if (< prob (rand)) A B)). Calling (A-B 0.5 new-feature control) gives each branch an approximately equal chance over many trials. section 5.10

Listing 5.6. → Random Numbers Generation
(rand
  ([])
  ([n]))

(rand-int
  [n])

Browse the compact function listing to compare rand’s arities with rand-int. section 5.10

How Clojure: The Essential Reference covers it

How deep it goes
Defines range, types, arities, and practical limits, then briefly identifies Java’s linear congruential generation approach.
Examples
Uses a 50-percent feature rollout and a two-branch A-B helper to demonstrate randomized decisions.
What it assumes
Assumes basic Clojure function and conditional syntax while defining the numeric contract directly.
What you can do after
Apply it via Clojure to make probability-based choices and randomized application behavior.
How it connects
Connects rand with rand-int, pseudorandom generation, A/B testing, and Java random-number facilities.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.