Julia でランダム文字列を生成する
Python で「ランダム文字列を生成する」
があったのですが。
結論
Julia では,以下のようにします。
が,もっといい方法 があります。
s = join(rand("abcde", 10))
"debcdddeba"
解説
rand(文字列, n)
で文字列に含まれる文字をランダムに n 個抽出します。
ch = rand("abcde", 10)
10-element Vector{Char}:
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'e': ASCII/Unicode U+0065 (category Ll: Letter, lowercase)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
join(文字ベクトル,区切り文字)
は文字ベクトルを区切り文字でつないで1つの文字列にします。
join(ch)
"bcebadcdcd"
join(['a', '9','#', 'あ'], ",")
"a,9,#,あ"
別解というか正統派の解
join(rand("abcde", 10))
の別解として,Julia では randstring( )
があります。
randstring("abcde", 10)
"bcaeacddab"
using Random
randstring('a':'z', 50)
"jjnzxrhaiohtnkkhjrxsjxyhzimbgnykwqksaxmcqfychndode"
randstring(['A', 'T', 'G', 'C'], 50)
"ATAAGTCCTTTTATAAGAGAGGGACAGAGTTCGCCATGCGGAACGCCTAT"
ちなみに
Julia の rand( )
の第1引数にはいろいろなものが指定できます。
文字列ベクトル
上述の文字ベクトルではなく,要素が文字列のベクトルの場合です。
s = rand(["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"], 10)
10-element Vector{String}:
"brown"
"The"
"lazy"
"brown"
"fox"
"The"
"fox"
"fox"
"over"
"dog"
join(s, " ")
"brown The lazy brown fox The fox fox over dog"
一様乱数 [0, 1)
rand(5)
5-element Vector{Float64}:
0.459597956997456
0.4264283858663884
0.5731704508950783
0.2736651506254155
0.20982345284583181
rand(2,4)
2×4 Matrix{Float64}:
0.152198 0.014708 0.351226 0.315778
0.431176 0.0660805 0.99479 0.199726
ちなみに,以下と同じです。
rand(Float64, (2, 4))
2×4 Matrix{Float64}:
0.0600171 0.734951 0.806253 0.654021
0.468655 0.502327 0.17355 0.143128
しかし,
rand((2, 4))
2
は,「2 か 4 のどちらかをランダムに(デフォルトで)1 個抽出する」ことを意味しますので,
rand((2, 4), 5)
5-element Vector{Int64}:
2
4
2
2
4
は,「2 か 4 のどちらかをランダムに 5 個抽出する」ことになります。
整数範囲
範囲を指定して整数乱数を生成できます。例えば,サイコロの出目(1〜6)をシミュレーションできます。
dice = rand(1:6, 6000)
using FreqTables
freqtable(dice)
6-element Named Vector{Int64}
Dim1 │
──────┼─────
1 │ 952
2 │ 992
3 │ 991
4 │ 1064
5 │ 1013
6 │ 988
なお,上の rand(Float64)
と同じように,整数全域での一様乱数を生成できます。
rand(Int, 5)
5-element Vector{Int64}:
1307378068556811867
1743341201359469284
-2376277955173019101
3553910491175235794
-8744920391893020888
rand(Int8, 5)
5-element Vector{Int8}:
50
-38
-96
50
5
種々の分布関数に従う乱数
分布関数を指定できます。例えば,母平均=50,母標準偏差=10 の正規分布にしたがう正規乱数を生成できます。
using Distributions, Statistics
x = rand(Normal(50, 10), 50000)
println("mean = $(mean(x)), sd = $(std(x, corrected=false))")
mean = 50.02290652115961, sd = 9.979379888695975
rand(Float64, 5)
5-element Vector{Float64}:
0.024355185626223852
0.2551396913231583
0.1893347993760104
0.5527201380146565
0.7468256571432557
rand(5)
5-element Vector{Float64}:
0.38966642825018405
0.23941693617145998
0.18881081853445392
0.7097366372930458
0.5113024978305314
非復元抽出
非復元抽出は乱数関数の引数で指定することが多いが,Julia では shuffle( ) を使います。
shuffle(1:10)
10-element Vector{Int64}:
7
10
3
9
6
1
2
8
5
4
shuffle(["foo", "bar", "baz", "moo", "goo"])
5-element Vector{String}:
"moo"
"goo"
"baz"
"bar"
"foo"