本文介绍了在 Julia 中生成范围内的随机整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从 MATLAB 迁移到 Julia,并尝试在 1:n
范围内生成一个随机整数.
I am migrating from MATLAB to Julia and I am trying to generate a random integer in range 1:n
.
对于 n ,
rand(r[1:n])
有效.
但是对于 n >20
,例如rand(r[1:21])
,我得到这样的信息:
However for n > 20
, for example, rand(r[1:21])
, I get this message:
ERROR: BoundsError() in getindex at range.jl:121
推荐答案
你可以给一个范围作为rand
的第一个参数,如rand(1:n)
:
You can give a range as the first argument to rand
, as in rand(1:n)
:
julia> rand(1:10)
7
julia> rand(1:10,10,10)
10x10 Array{Int64,2}:
10 2 5 8 5 5 3 7 1 3
5 1 4 2 4 4 1 6 6 9
8 1 3 9 4 8 7 8 7 10
3 8 1 5 7 9 7 8 10 7
5 8 5 6 6 2 2 7 4 3
10 4 8 8 10 5 1 10 5 1
6 1 8 1 6 5 7 10 6 10
5 10 2 5 4 5 4 1 3 9
5 4 6 4 4 1 7 8 1 5
10 2 6 4 3 10 7 3 8 7
通用 rand
函数的第一个参数通常给出一个要从中采样的东西",可以是一个值范围,也可以是 Distributions.jl.
The first argument to the general rand
function usually gives a "thing to sample from", be it a range of values or a distribution object as defined in Distributions.jl.
这篇关于在 Julia 中生成范围内的随机整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!