automate tuple creation
Ali Çehreli
acehreli at yahoo.com
Wed Jan 19 22:33:02 UTC 2022
On 1/19/22 13:59, forkit wrote:
> void createBoolMatrix(ref uint[][] m)
> {
> auto rnd = Random(unpredictableSeed);
That works but would be unnecessarily slow and be against the idea of
random number generators. The usual approach is, once you have a
randomized sequence, you just continue using it. For example, I move rnd
to module scope and initialize it once.
Random rnd;
shared static this() {
rnd = Random(unpredictableSeed);
}
auto randomValue() {
return cast(uint)rnd.dice(0.6, 1.4);
}
// Returning a dynamically allocated array looks expensive
// here. Why not use a struct or std.typecons.Tuple instead?
auto randomTuple() {
return [ randomValue(), randomValue() ];
}
void createBoolMatrix(ref uint[][] m, size_t count)
{
import std.algorithm : map;
import std.range : iota;
m = count.iota.map!(i => randomTuple()).array;
}
Ali
More information about the Digitalmars-d-learn
mailing list