From 4e290d282f635047e305ad75fe72db582b408516 Mon Sep 17 00:00:00 2001 From: Avikant Srivastava Date: Tue, 16 Jan 2024 21:02:28 +0530 Subject: [PATCH] feat: add time based seed to random.h (#457) * random seed from time * fix: chrono * refactor: snake case --- mlx/random.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mlx/random.h b/mlx/random.h index 360bdbdb1..ab75eb488 100644 --- a/mlx/random.h +++ b/mlx/random.h @@ -2,6 +2,7 @@ #pragma once +#include #include #include "mlx/array.h" @@ -18,12 +19,18 @@ class KeySequence { // static default static KeySequence& default_() { - static KeySequence ks(0); + static KeySequence ks(get_current_time_seed()); return ks; } private: array key_; + static uint64_t get_current_time_seed() { + auto now = std::chrono::system_clock::now(); + return std::chrono::duration_cast( + now.time_since_epoch()) + .count(); + } }; /** Get a PRNG key from a seed. */