liveBook · Manning

Crypto/rand package

As explained in Learn Go with Pocket-Sized Projects

crypto/rand is Go's standard-library package for generating cryptographically secure random values from system entropy sources.

crypto/rand is Go’s standard-library package for generating cryptographically secure random values from system-level entropy sources. It costs more than math/rand because it accesses hardware or the operating system, making it appropriate for passwords, tokens, and other security-related objects. section 5.3.3

Choose between Go’s random-number packages according to the consequences of predictable or expensive output:

  • Use crypto/rand for passwords, tokens, cryptographic data, and security-related objects. section 5.6
  • Use math/rand for small, noncritical applications when lower cost matters more than cryptographic randomness. section 5.3.3
  • Use Intn(n) to obtain an integer from zero through n-1, then use it as an index into a corpus. section 5.3.3

Selecting a word for Gordle

A word game can select a random corpus entry by generating an index with Intn(len(corpus)) and returning corpus[index]. The result can then become the game’s solution; tests verify that the returned word belongs to the corpus. section 5.3.3

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains entropy sources, cost, package choice, bounded integers, and corpus selection, but does not descend into implementation internals.
Examples
Uses random word selection in a terminal word game and discusses random identifiers for games exposed through endpoints.
What it assumes
Assumes familiarity with Go packages, random-number APIs, and indexing a slice or corpus.
What you can do after
Apply it via Go’s standard library when generating security-related values or choosing randomness with stronger guarantees.
How it connects
Connects crypto/rand with math/rand, system entropy, random number generators, passwords, tokens, UUIDs, and ULIDs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.