isocpp / isocpp/CppCoreGuidelines

Random numbers section

Open
#869 8 comments 0 reactions 1 assignee View on GitHub

@jwakely is already working on this.

Since Mar 27, 2017.

Dominant language
CSS
Stars
45.3k
Forks
5.6k
PR merge metrics
No merged PRs in 30d

Description

Suggestion for the SL: The Standard Library: could it discuss best practices w.r.t pseudo-random number generation? Something like

  • avoid rand() (explicitly discouraged by the standard itself, also CERT rule 12.1 MSC50-CPP), use the <random> facilities instead
  • seed random number generators appropriately (CERT rule 12.2 MSC51-CPP bans default-init and use of current time, and endorses std::random_device, famously non-random in mingw. I am not sure it's worth getting into the whole discussion of entropy, PRNG state size, std::seed_seq, etc, but a simple guideline could do good if it can be formulated)
  • do not use modulo operator to resize a uniform distribution (it makes it non-uniform; use std::uniform_int_distribution)
  • use std::bernoulli_distribution to generate random bits (because a separate call to RNG for every bit, as some people do, is a waste) - maybe not, that's probably not detectable
  • use a special-purpose library when using random numbers for cryptographic purposes... okay that's not detectable either.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.