Logo

Programming-Idioms

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

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.

with Ada.Numerics.Discrete_Random;
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;

Range includes both A and B

New implementation...
< >
programming-idioms.org