Logo

Programming-Idioms

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.
Implementation
C

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another C implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import java.util.Random;
Random r = new Random(s);
import "math/rand"
r := rand.New(rand.NewSource(s))
srand($s);
std.random, std.stdio;
auto s = 8;
auto gen = Random(s);
writeln(gen.front);
var
  SomeInteger: Integer;
  Value: double;
begin
  ...
   //initializes the PRNG's seed with a value depensing on system time
  Randomize; 
  Value := random;
  ...
   //Output will be the same eacht time the program runs
  RandSeed := SomeInteger; 
  Value := random;
...
end.
import "dart:math";
var r = new Random(s);
r = Random.new(s)
System.Random.mkStdGen s
import random
rand = random.Random(s)
srand($s);
math.randomseed(s)
use rand::{Rng, SeedableRng, rngs::StdRng};
let s = 32;
let mut rng = StdRng::seed_from_u64(s);
const seed = require ('seedrandom')
seed (s)
using System;
var random = new Random(s);
call random_seed (put=s)
  call random_seed(size = n)
  allocate(seed(n))
  ! ...
  call random_seed(put=seed)
#include <stdlib.h>
srand(s);
val random = Random(seed=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;
:random.seed(s)