# Programming-Idioms

This language bar is your friend. Select your favorite languages!

# Idiom #11 Pick a random element from a list

List x must be non-empty.

`(nth (random (length x)) x)`
`(nth (random (length x)) x)`
`#include <stdlib.h>`
`x[rand() % x_length];`
`(rand-nth x)`
`#include <random>`
```std::mt19937 gen;
std::uniform_int_distribution<size_t> uid (0, x.size () - 1);
x[uid (gen)];
```
```using System;
using System.Collections.Generic;```
`x[new Random().Next(x.Count)];`
`import std.random;`
`x.randomSample(1);`
`x[new Random().nextInt(x.length)];`
```Enum.random(x)
```
`lists:nth(rand:uniform(length(X)), X).`
`https://github.com/inaka/erlang-katana`
`ktn_random:pick(X)`
```call random_number (a)
x(lbound(x) + int(a*ubound(x))
```
`import "math/rand"`
`x[rand.Intn(len(x))]`
`import "math/rand"`
```func pickT(x []T) T {
return x[rand.Intn(len(x))]
}```
`import System.Random (randomR)`
`(\l g -> l !! fst (randomR (0, length l) g))`
`import System.Random (randomRIO)`
`(l !!) <\$> randomRIO (0, length l - 1)`
```const x = [1, 'two', 3, 4, 'five', 'six', 7, 8, 'nine' ];

const randomElement = x[~~(Math.random() * x.length)];```
`x[Math.floor(Math.random() * x.length)]`
`import java.util.concurrent.ThreadLocalRandom;`
`x.get(ThreadLocalRandom.current().nextInt(0, x.size()))`
`x.get((int)(Math.random()*x.size()))`
`list.random()`
`x[math.random(#x)]`
`\$x[ array_rand(\$x) ]`
`uses classes;`
`element := x.Items[random(x.count)];`
```element := x[random(length(x))];
```
```my @x = ('a', 'list', 'of', 'random', 'items');
print \$x[rand @x];```
`import random`
`random.choice(x)`
`x.sample`
`use rand::{self, Rng};`
`x[rand::thread_rng().gen_range(0, x.len())]`
```extern crate rand;
```let choice = thread_rng().choose(&x).unwrap();
`import scala.util.Random`
```val x = List(1, 2, 3, 4)