logo

Sampling Randomly in Julia 📂Julia

Sampling Randomly in Julia

Description1

In Julia, the function for random sampling is as follows:

  • rand([rng=default_rng()], [S], [dims...])

rng stands for Random Number Generator, which specifies the random number generation algorithm. If you don’t understand what this means, it’s okay to leave it untouched.

S likely stands for Set, and it is a variable that specifies the set from which the random sampling will occur. The variables that can be input for S include the following:

  • Objects with indices
  • AbstractDict or AbstractSet
  • Strings
  • Types (only integers and floating points are possible. Rational and irrational numbers are not allowed.)

When the extraction set is specified as a type, if it’s an integer type, random numbers are drawn from the range of typemin(S):type(S) (does not support BigInt).

julia> typemin(Int16), typemax(Int16)
(-32768, 32767)

julia> typemin(Int32), typemax(Int32)
(-2147483648, 2147483647)

julia> typemin(Int64), typemax(Int64)
(-9223372036854775808, 9223372036854775807)

If it’s a floating-point, random numbers are drawn from the range of $[0, 1)$.

julia> rand(Float64)
0.4949745522302659

julia> rand(ComplexF64)
0.8560168003603014 + 0.16478582700545064im

[dims...] denotes the dimensions of the array to be drawn. If it’s rand(S, m, n), it draws $m \times n$ elements (including duplicates) from the set S and returns an array of shape $m \times n$. If dimensions are not specified, a real number is returned. Be careful because real numbers and 1-dimensional vectors are distinctly differentiated. Also, be careful when you wish to obtain an array of shape $2\times 3$ and input the dimensions as a tuple like (2,3), as it gets treated as a variable for S, yielding a completely different result.

julia> rand(Float64)               # 실수 추출
0.42226201756172266

julia> rand(Float64, 1)            # 성분이 실수인 1x1 배열로 추출
1-element Vector{Float64}:
 0.7361136057571305

julia> rand(2,3)                   # 성분이 실수인 2x3 배열로 추출 
2×3 Matrix{Float64}:
 0.648742   0.364548  0.0550352
 0.0350098  0.56055   0.83297

julia> rand((2,3))                 # 2와 3중에서 추출
3

For more advanced content, refer to the following:

Code

Objects with Indices

julia> rand((2,5))
5

julia> rand(2:5)
3

julia> rand([2,3,4,5])
4

julia> rand(["x", "y", 4])
"x"

Dictionary

If the sampling set is a dictionary, the key-value pair itself is extracted.

julia> d = Dict(2=>4, 3=>5, 4=>"6")
Dict{Int64, Any} with 3 entries:
  4 => "6"
  2 => 4
  3 => 5

julia> rand(d)
4 => "6"

julia> rand(d)
2 => 4

String

If the sampling set is a string, a random character from the string is extracted.

julia> str = "freshrimpsushi"
"freshrimpsushi"

julia> rand(str)
'e': ASCII/Unicode U+0065 (category Ll: Letter, lowercase)

julia> rand(str)
'h': ASCII/Unicode U+0068 (category Ll: Letter, lowercase)

Type

julia> rand(Int32, 3)
3-element Vector{Int32}:
  1552806175
  -384901411
 -1580189675

julia> rand(UInt32, 3)
3-element Vector{UInt32}:
 0xd2f44f99
 0x166a8b9e
 0x92fe22dc

julia> rand(Float32, 3)
3-element Vector{Float32}:
 0.59852564
 0.6247238
 0.23303497

julia> rand(ComplexF32, 3)
3-element Vector{ComplexF32}:
 0.10872495f0 + 0.6622572f0im
  0.6408408f0 + 0.46815878f0im
  0.7766515f0 + 0.73314756f0im

Environment

  • OS: Windows11
  • Version: Julia 1.9.0