How to generate a random number from system clock as seed
monkyyy
crazymonkyyy at gmail.com
Sat Jun 8 16:09:04 UTC 2024
On Saturday, 8 June 2024 at 13:19:30 UTC, Eric P626 wrote:
> I managed to create a random number generator using the
> following code:
>
> ~~~
> auto rng = Random(42);
> //....
> uniform(0,10,rng);
> ~~~
>
> Now I want to seed the generator using system time. I looked at
> Date & time functions/classes and systime functions/classes.
> The problem is that they all require a time zone. But I don't
> need a time zone since there is no time zone. I just want the
> number of seconds elapsed since jan 1st 1970. In other words,
> the internal system clock value.
rng is an optional parameter, `uniform(0,100).writeln;` alone
works; the docs not telling you that is really bad
the docs/api for std.time/random are bad if you need something
specif Id suggest doing it yourself, but if you want to use
std.time anyway the magic word I think is "localtime"(Ive pounded
my head into those auto generated docs and had to dive deep to
find such estoric knowledge)
if you need a spefic random number from a spefic timestamp, Id
suggest making a rng function from scratch and using clibs time
stuff
More information about the Digitalmars-d-learn
mailing list