Logo

Programming-Idioms

  • Rust
  • Fortran

Idiom #69 Seed random generator

Use seed s to initialize a random generator.

If s is constant, the generator output will be the same each time the program runs. If s is based on the current value of the system clock, the generator output will be different each time.

  call random_seed(size = n)
  allocate(seed(n))
  ! ...
  call random_seed(put=seed)

seed is an allocatable variable.
use rand::{Rng, SeedableRng, rngs::StdRng};
let s = 32;
let mut rng = StdRng::seed_from_u64(s);
with Ada.Numerics.Discrete_Random;
declare
   Package Rand is new Ada.Numerics.Discrete_Random (Integer);
   Gen : Rand.Generator;
begin
   Rand.Reset (Gen, Initiator => S);
end;

You can also Save the state of the generator and Reset with that state later to continue the (pseudo-random) sequence.

New implementation...