liveBook · Manning

Rand-int

As explained in Clojure: The Essential Reference

rand-int is a Clojure function that returns a pseudorandom integer from zero up to, but not including, a specified integer bound.

rand-int returns a pseudorandom integer below a supplied integer bound. It requires one argument, produces an integer, and uses Clojure’s Java-backed pseudorandom generation; this is suitable for many practical uses, but not stronger randomness requirements. section 5.10

Generate a bounded integer

(rand-int 10) ;; 0 through 9 (rand-int -10) ;; -5 The bound must be an integer; rand-int has no zero-argument form. The negative-bound example demonstrates that negative bounds are accepted, while the exact result remains unpredictable. section 5.10

Use rand-int when the random number itself matters, especially when it will control an index. For selecting an element from a collection, rand-nth packages the same idea; use rand-int with nth when explicit index control is useful. section 8.3.1

How Clojure: The Essential Reference covers it

How deep it goes
Defines the input, output, range behavior, and pseudorandomness, but stops short of implementation internals for rand-int.
Examples
Uses bounded calls, a negative bound, and random collection indexing as concrete examples.
What it assumes
Assumes familiarity with Clojure function calls and integer bounds while distinguishing rand’s double output from rand-int’s integer output.
What you can do after
Apply it via Clojure to generate a pseudorandom integer or control an explicit collection index.
How it connects
Connects rand-int with rand, rand-nth, nth, shuffle, vectors, and lazy sequences.

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.