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

Idiom #70 Use clock as random generator seed

Get the current datetime and provide it as a seed to a random generator. The generator sequence will be different at each run.

srand time;
#include <stdlib.h>
#include <time.h>
srand((unsigned)time(0));
import std.datetime, std.random;
auto rng = Random(cast(uint)Clock.currTime.stdTime);
import std.datetime, std.random;
rndGen.seed(cast(uint)Clock.currTime.stdTime);
var r = new Random(new DateTime.now().millisecondsSinceEpoch);
import "math/rand"
import "time"
r := rand.New(rand.NewSource(time.Now().UnixNano()))
import "math/rand"
import "time"
rand.Seed(time.Now().UnixNano())
import System.Random
getStdGen
import java.util.Random;
Random rand = new Random(System.currentTimeMillis());
math.randomseed( os.time() )
srand(time());
echo rand();
Randomize;
import random
rand = random.Random()
Random.new

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

deleplace

Related idioms