Fast random number generator for RGBs

This is a bit simpler than f2, while retaining the same performance:

julia> @btime f2();
  27.606 ms (2 allocations: 24.47 MiB)

julia> function f3()
          M = Matrix{RGB{Float32}}(undef, 1080, 1980)
          @. M = RGB(rand(), rand(), rand())
          return M
       end
f3 (generic function with 1 method)

julia> @btime f3();
  28.184 ms (2 allocations: 24.47 MiB)

ButiIf you want performance you need to explicitly pass the RNG:

julia> rng = Random.default_rng()
MersenneTwister(0x1367e9fe8e33ee99f87edfd09c2e5904, (0, 7916357112, 7916356110, 690))

julia> function f4(rng)
          M = Matrix{RGB{Float32}}(undef, 1080, 1980)
          @. M = RGB(rand(rng), rand(rng), rand(rng))
          return M
       end
f4 (generic function with 1 method)

julia> @btime f4($rng);
  11.616 ms (2 allocations: 24.47 MiB)

julia> using RandomNumbers

julia> rng_xor = RandomNumbers.Xorshifts.Xoroshiro128Star()
RandomNumbers.Xorshifts.Xoroshiro128Star(0x5f9b1be270242ed9, 0x4ddeffa1af6dbc23)

julia> @btime f4($rng_xor);
  11.991 ms (2 allocations: 24.47 MiB)
5 Likes