Idiom #15 Pick uniformly a random integer in [a..b]
Pick a random integer greater than or equals to a, inferior or equals to b. Precondition : a < b.
Pick a random integer greater than or equals to a, inferior or equals to b. Precondition : a < b.
int pick(int a, int b)
{
int upper_bound = b - a + 1;
int max = RAND_MAX - RAND_MAX % upper_bound;
int r;
do {
r = rand();
} while (r >= max);
r = r % upper_bound;
return a + r;
}
Random.Shared.Next(a, b + 1)
function pick(a, b) {
return a + Math.floor(Math.random() * (b - a + 1));
}
std::mt19937 gen; std::uniform_int_distribution<size_t> uid (a, b); uid (gen);
int pick(int a, int b) => a + Random().nextInt(b - a + 1);
declare subtype Random_Range is Integer range A .. B; package Rand is new Ada.Numerics.Discrete_Random (Random_Range); use Rand; Gen : Generator; Result : Random_Range; begin Reset (Gen); Result := Random (Gen); end;
int pick(int a, int b) { int upper_bound = b - a + 1; int max = RAND_MAX - RAND_MAX % upper_bound; int r; do { r = rand(); } while (r >= max); r = r % upper_bound; return a + r; }
(+ a (rand-int (- b a)))
Random r = new Random(); return r.Next(a, b + 1);
Random.Shared.Next(a, b + 1)
auto x1 = uniform(a, b+1); auto x2 = uniform!"[]"(a, b);
a - 1 + :rand.uniform(b-a+1)
:crypto.rand_uniform(a, b)
crypto:rand_uniform(A, B)
real :: c integer :: res call random_number(c) res = int((b-a+1)*c)
func pick(a, b int) int { return a + rand.IntN(b-a+1) }
func pick(a,b int) int { return a + rand.Intn(b-a+1) }
let pick (a, b) = randomRIO (a, b) :: IO Integer in pick (1, 6) >>= print
function pick(a, b) { return a + Math.floor(Math.random() * (b - a + 1)); }
int pick(int a, int b){ return a + new Random().nextInt(b - a + 1); }
int v = new Random().nextInt(a, b + 1);
fun pick(a: Int, b: Int): Int { return (a..b).random() }
(defun r (a b) (+ a (random (+ 1 (- b a )))))
math.random(a, b)
a+arc4random_uniform(b+1)
rand($a, $b)
var _a, _b, _r: Integer; begin _r := RandomRange(_a, _b); end;
my ($min, $max) = (5, 25); my $val = $min + int(rand($max-$min));
random.randint(a,b)
rand(a..b)
Uniform::new_inclusive(a, b).sample(&mut rand::thread_rng())
fn pick(a: i32, b: i32) -> i32 { let between = Range::new(a, b); let mut rng = rand::thread_rng(); between.ind_sample(&mut rng) }
a + Random.nextInt(b + 1)
(floor (+ a (* (add1 (- b a)) (random 1.0))))
(+ a (random (add1 (- b a))))
(a to: b) atRandom