Julia: rand

来源:互联网 发布:数据库题库 编辑:程序博客网 时间:2024/04/17 00:20

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.388394julia> rand(1:5)1

二、生成一个区间的整数的随机数

julia> rand(1:5,2)2-element Array{Int64,1}: 1 1julia> rand(1:5,2,2)2×2 Array{Int64,2}: 2  3 4  5julia> 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.0julia> rand(0.5:5.0,2,2)2×2 Array{Float64,2}: 4.5  4.5 4.5  3.5julia> 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)]8julia> group[rand(1:len)]1julia> group[rand(1:len)]0julia> group[rand(1:len)]9julia> group[rand(1:len)]1
0 0