つらつら Excel VBA

私の備忘録です。

ランダム値の生成

'ランダム値を取得
Function getRandom(i_min As Integer, i_max As Integer) As Integer
   
    Dim temp As Integer
    Dim sa As Integer '差
   
    Randomize '乱数生成ジェネレータ
   
    '引数が逆だった場合は入れ替え。
    If i_max < i_min Then
        temp = i_max
        i_max = i_min
        i_min = temp
    End If
   
    sa = i_max - i_min + 1 '差分を計算
    getRandom = Int(sa * Rnd) + i_min
   
End Function