Logo

Programming-Idioms

  • Java
  • Rust

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.

use rand::{Rng, SeedableRng, rngs::StdRng};
use std::time::SystemTime;
let d = SystemTime::now()
    .duration_since(SystemTime::UNIX_EPOCH)
    .expect("Duration since UNIX_EPOCH failed");
let mut rng = StdRng::seed_from_u64(d.as_secs());
import java.util.Random;
Random rand = new Random(System.currentTimeMillis());
#include <stdlib.h>
#include <time.h>
srand((unsigned)time(0));

New implementation...