Random number distributions
Issue #158 resolved
What would be the best way to integrate (other than uniform) random number distributions (i.e. normal, binomial, etc.) with blaze's existing random number generation scheme? I looked around in the code and it looks like that there is currently no way of changing the default (uniform) distribution.
Any help would be greatly appreciated.
Thanks for creating this issue. You are correct, currently there is no way to change the kind of distribution for the random number generation. However, admittedly, we did not anticipate that it needed to be changed. We will look into this issue and try to integrate a way to customize the distribution, but only after the release of Blaze 3.3.
This could be reasonably simple. In my experiments, the fastest I've been able to generate these numbers has been using an unrolled AES-CTR RNG which is here (https://github.com/dnbaker/frp/blob/master/include/frp/aesctr.h). By using an UNROLL_COUNT of 8, I've gotten about a 5% speedup from the default 4.
My generic solution for a related problem was here:
One could call
DISTRIBUTION_fill(matrix, seed, [... constructor arguments for the distribution...]). I currently use header-only components of boost because they're faster on my machine than the libstdc++ implementations, but I could fall back to
I'd need to add a case overload for sparse and static matrices, but it isn't terribly far from being a solution, if perhaps not optimally performant.
And specifically for the DenseMatrix case, one could instead copy the values by SIMD, and I think an SIMDEnabled-based functor might be the better way to do it. I'd be willing to put in some work for it.
Thank a lot for your code sample and the offer to put some work into it. However, you might have noticed that Blaze already has the functionality to randomize vectors and matrices (see the complete documentation). Hartmut's request is to extend the existing interface by providing the opportunity to pass different kinds of distribution (similar to the
std::shuffle()algorithm). Although your solution would work in general it would be a considerable effort to change everything. Extending the existing solution is therefore preferable. Thanks again,
This feature request is resolved by the introduction of vector and matrix generators (see also issue
#313). Generators have been implemented, tested, optimized, and documented as required. They are immediately available via cloning the Blaze repository and will be officially released in Blaze 3.7.
generate()function returns a dense vector filled elementwise via the given custom operation. By default, the returned vector is a column vector, but this setting can be changed via the
BLAZE_DEFAULT_TRANSPOSE_FLAGswitch (see Default Vector Storage). Alternatively it is possible to specify the transpose flag explicitly.
The following example demonstrates the use of the generate() function:
Custom Random Generators
generate()function it is possible to create custom generators. The following example demonstrates this by means of the