On 09.06.2024 16:37, Eric P626 wrote:
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;
    }
}
~~~

I managed to use this piece of code and it worked.

~~~
uint seed = castFrom!long.to!uint(Clock.currStdTime);
auto rng = Random(seed);
~~~

I am not exactly sure what the exclamation points stand for in the first line of code. Probably, defining a type to class ```castFrom``` and function ```to```. But I get the idea that it just cast long to uint since ```Random``` requires an unsigned int > I assume from this line of code that C casting like ```(uint)varname``` would not work.




`CastFrom!long` is a template instantiation. The instantiated template converts from `long` to `uint`. It is useful in meta-programming but here you can safely use other forms like c style cast:
```d
    const seed = cast(uint) Clock.currStdTime;
```
or
```
    const seed = to!uint(Clock.currStdTime & 0xFFFF);
```
here we need to use the `0xFFFF` mask, because `to!uint` throws a conversion positive overflow when the value of `Clock.currStdTime` exceeds the value of `uint.max`. But I don't like this variant and I don't recommend it.

Reply via email to