rand, 是Julia中一个经常用到的随机数生成函数。
Now,is adding Julia working paths,and packages......
path arrangement and using package are finished! costed: 0.6589999198913574 seconds
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.5.0 (2016-09-19 18:14 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-w64-mingw32
一、生成一个(0,1)的均匀分布的随机数
julia> rand(5)
5-element Array{Float64,1}:
0.678022
0.392145
0.914351
0.909664
0.388394
julia> rand(1:5)
1
二、生成一个区间的整数的随机数
julia> rand(1:5,2)
2-element Array{Int64,1}:
1
1
julia> rand(1:5,2,2)
2×2 Array{Int64,2}:
2 3
4 5
julia> rand(1:5,10,10)
10×10 Array{Int64,2}:
4 2 4 1 1 1 2 3 3 1
2 1 4 1 4 1 4 4 1 5
3 5 1 5 5 3 2 2 3 3
2 3 4 4 5 4 2 3 1 2
1 3 1 3 3 2 2 4 5 3
3 1 1 4 2 1 5 5 3 4
3 3 4 1 1 5 3 1 1 2
5 1 1 3 3 1 4 4 2 5
2 2 1 1 4 2 5 2 3 3
1 5 1 3 5 1 5 5 5 2
如果不是整数呢,会如何? 大家看出什么?
julia> rand(1.0:5.0,2,2)
2×2 Array{Float64,2}:
2.0 5.0
3.0 3.0
julia> rand(0.5:5.0,2,2)
2×2 Array{Float64,2}:
4.5 4.5
4.5 3.5
julia> rand(0.51:5.25,2,2)
2×2 Array{Float64,2}:
2.51 2.51
3.51 0.51
三、随机选出一个数组内元素
julia> group =[1,3,5,7,8,9,10,13,15,17,19,21,0,1,3,5,48,6,7];
julia> len =length(group);
julia> group[rand(1:len)]
8
julia> group[rand(1:len)]
1
julia> group[rand(1:len)]
0
julia> group[rand(1:len)]
9
julia> group[rand(1:len)]
1
四、随机选出N个数
julia> rand([1,2,3,4],2) #从其中选出,2个数
2-element Array{Int64,1}:
3
1
julia> rand([1.0,2.0,3.1,4.1],2)
2-element Array{Float64,1}:
2.0
3.1
julia> rand(1:1:6,2)
2-element Array{Int64,1}:
1
3