Random Tuple

How do I create a random Tuple in Julia 1.2. In the docs it says
" Support for S as a tuple requires at least Julia 1.1."
but when I try rand(Tuple{Int64, Int64}) I get the error “Sampler for this object is not defined”

julia> map(rand, (Int64, Int64))
(8612835158694908284, -1644040312126833100)
3 Likes

This refers to

Pick a random element or array of random elements from the set of values specified by S; S can be … a tuple

julia> rand((1,6))
6

julia> rand((1,6))
1
2 Likes

An alternative to map(rand, (Int64, Int64)), if you don’t mind using a package:

julia> using RandomExtensions

julia> rand(Tuple{Int,Int})
(6826736764378320271, -5931137627310704668)

julia> rand(NTuple{3}) # Float64 by default
(0.08253834652530578, 0.9455169860381427, 0.9683800214432958)

julia> rand(1:9, Tuple, 5)
(3, 6, 9, 1, 4)
4 Likes

Thanks for the quick response.

What is a version with specific random number generator? Say, I want to use:

rng = MersenneTwister(314159);

The same way you take any other scalar operation and apply it to multiple arguments: broadcasting!

julia> using Random

julia> rng = MersenneTwister(314159);

julia> rand(rng, Int64)
-3407443199336529215

julia> rand.(rng, (Int64, Int64))
(-6900968445183223651, 2301483206877765760)

julia> rand.(rng, (Int64, Float64, UInt8))
(1901333683407452888, 0.12793828142902886, 0xfa)
2 Likes

I meant, how do you use it with map function call?

Here’s one way?

map(x -> rand(rng,x), (Int64, Int64))
2 Likes