Logo

Programming-Idioms

History of Idiom 11 > diff from v67 to v68

Edit summary for version 68 by fantasy:
New Cpp implementation by user [fantasy]

Version 67

2018-04-17, 16:50:12

Version 68

2018-05-08, 01:23:30

Idiom #11 Pick a random element from a list

List x must be non-empty.

Illustration

Idiom #11 Pick a random element from a list

List x must be non-empty.

Illustration
Extra Keywords
choose
Extra Keywords
choose
Imports
#include <random>
Code
std::mt19937 gen;
std::uniform_int_distribution<size_t> uid (0, x.size () - 1);
x[uid (gen)];
Comments bubble
Assuming that x.size () > 0