Jump to page: 1 2
Thread overview
How to generate a random number from system clock as seed
Jun 08
Eric P626
Jun 08
monkyyy
Jun 08
monkyyy
Jun 09
Eric P626
Jun 09
monkyyy
Jun 09
Eric P626
Jun 08
drug007
Jun 09
Eric P626
Jun 09
drug007
Jun 10
Eric P626
June 08

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.

June 08
On Saturday, 8 June 2024 at 13:19:30 UTC, Eric P626 wrote:
> I just want the number of seconds elapsed since jan 1st 1970. In other words, the internal system clock value.

#unix #time

@SDB79

June 08

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

June 08
On 08.06.2024 16:19, 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.
> 

```d
import std;

void main()
{
    {
        auto rng = Random(42);
        auto result = generate!(() => uniform(0, 10, rng))().take(7);
	// the same random numbers sequence
        assert (result.equal([2, 7, 6, 4, 6, 5, 0]));
    }

    {
        const seed = castFrom!long.to!uint(Clock.currStdTime);
        auto rng = Random(seed);
        auto result = generate!(() => uniform(0, 10, rng))().take(7);
	// new random numbers sequence every time
        result.writeln;
    }
}
```
June 08

On Saturday, 8 June 2024 at 18:25:20 UTC, drug007 wrote:

>
    {
        const seed = castFrom!long.to!uint(Clock.currStdTime);
        auto rng = Random(seed);
        auto result = generate!(() => uniform(0, 10, rng))().take(7);
	// new random numbers sequence every time
        result.writeln;
    }

If UnixTime is desired, it is sufficient to have currTime.toUnixTime instead of currStdTime. It will not reset until January 19, 2038.

auto seed = to!uint(Clock.currTime.toUnixTime);

SDB@79

June 08

On Saturday, 8 June 2024 at 16:09:04 UTC, monkyyy wrote:

>

rng is an optional parameter, uniform(0,100).writeln; alone works; the docs not telling you that is really bad

They do tell you:

>

urng (optional) random number generator to use; if not specified, defaults to rndGen

That overload is used in the 2nd example.

https://dlang.org/phobos/std_random.html#.uniform

June 08

On Saturday, 8 June 2024 at 20:53:02 UTC, Nick Treleaven wrote:

>

On Saturday, 8 June 2024 at 16:09:04 UTC, monkyyy wrote:

>

rng is an optional parameter, uniform(0,100).writeln; alone works; the docs not telling you that is really bad

They do tell you:

>

urng (optional) random number generator to use; if not specified, defaults to rndGen

That overload is used in the 2nd example.

https://dlang.org/phobos/std_random.html#.uniform

generate is a very rare function and do novices understand lamdas?

June 09

On Saturday, 8 June 2024 at 21:04:16 UTC, monkyyy wrote:

>

generate is a very rare function and do novices understand lamdas?

Yes I know lamdas, but try not to use them.

I am not very picky about the exact source of time, I just want a different integer every time I run the program. But while looking at the doc, it seemed complicated because I required time zones, and I could not get the entire time stamp as a single integer.

June 09

On Saturday, 8 June 2024 at 13:19:30 UTC, Eric P626 wrote:

>

Now I want to seed the generator using system time.

Just to be clear, do you specifically want to use the system time, or are you aiming to use the system time to generate different seeds for each run?

If the latter you might prefer to use the unpredictableSeed() function from std.random:
https://dlang.org/phobos/std_random.html#unpredictableSeed

That may use a variety of different methods to generate the seed, depending on the capabilities of the system, but one of the fallback options (which is actually the original implementation IIRC) uses a pseudo-random transformation of values derived from the thread ID, the process ID, and the current time in ticks as reported by the system's monotonic clock.

A benefit of that is that it means that different threads in the same app, and different apps, are guaranteed to get different random seeds if started at the same time.

The preferred alternatives to that use different mechanisms, but should all have the same desired property of guaranteeing that different threads get different random seeds.

But FWIW, if current time in ticks is valid for your use-case, cast(ulong) MonoTime.currTime.ticks should do.

Hope that helps!

Best wishes,

-- Joe
June 09

On Saturday, 8 June 2024 at 16:09:04 UTC, monkyyy wrote:

>

rng is an optional parameter, uniform(0,100).writeln; alone works; the docs not telling you that is really bad

The docs do tell you that rng is an optional parameter of uniform:
https://dlang.org/phobos/std_random.html#uniform

However, there are often good reasons to want to pass a specific RNG: you may want to pick the specific RNG algorithm, and you may want to be able to know exactly how it was seeded, e.g. if you want to be able to reproduce the same results. I assume that Eric's use case may reflect that.

« First   ‹ Prev
1 2