在Julia中生成随机数

huangapple go评论90阅读模式
英文:

Generating random numbers in Julia

问题

在Julia中如何生成范围在[-1, 1]之间的真正随机数?

抱歉,我刚开始在Julia中工作,但找不到正确的答案。例如,我尝试了以下代码:

rand(Float16, [-1,1], 1)

但每次都会出现ERROR: MethodError

英文:

How to generate a real random number in range [-1, 1] in Julia?

Sorry, I'm just starting working in Julia and I couldn't find a right answer. For example I tried

rand(Float16, [-1,1], 1)

but I get ERROR: MethodError every time.

答案1

得分: 3

rand() 返回一个 [0, 1) 范围内的随机数,所以你可以做的最简单的事情是

2rand() - 1

当你向 rand(x) 提供一个参数时,通常意味着类似 "从 x 中返回一个随机样本"。例如,rand(1:10) 意味着 "随机选择一个介于1和10(包括1和10)之间的整数:

julia> rand(1:10)
2

rand 的第二个位置参数是样本数,所以 rand(1:10, 3) 会随机选择3个数字(可以有重复):

julia> rand(1:10, 3)
3个整数向量:
  6
  5
 10

如果你想更明确地说明你要抽样的分布,可以使用 Distributions 包:

julia> using Distributions

julia> rand(Uniform(-1, 1), 3)
3个Float64向量:
  0.13509110532903756
 -0.026048031817401895
 -0.5490076326320161

在这里,我们将一个 Distribution 对象作为第一个参数传递,这意味着 rand 将根据 Distributions 中定义的概率密度函数从该分布中抽样。

关于浮点数运算的限制,我们可以继续讨论很长时间。Julia社区有一些关于这个话题的深入讨论,这是我随机找到的一个讨论(非恶意随机):

https://discourse.julialang.org/t/how-to-create-a-random-uniform-distribution-between-but-excluding-0-and-10/21908/2

英文:

rand() returns a random number in [0, 1), so the easiest thing you could do is

2rand() - 1

When you give an argument to rand(x) it generally means something like "return a random sample from x. As an example, rand(1:10) means "pick an integer between 1 and 10 (inclusive) at random:

julia> rand(1:10)
2

The second positional argument to rand is the number of samples, so rand(1:10, 3) will pick 3 random numbers (with replacement):

julia> rand(1:10, 3)
3-element Vector{Int64}:
  6
  5
 10

If you wanted to be more explicit about the distribution you're sampling from you could use the Distributions package:

julia> using Distributions

julia> rand(Uniform(-1, 1), 3)
3-element Vector{Float64}:
  0.13509110532903756
 -0.026048031817401895
 -0.5490076326320161

Here we pass a Distribution object as the first argument, which means rand will sample from this distribution according to the pdf defined in Distributions.

We could go on for quite a bit longer as your question turns out to be quite a deep one when one considers the limitations of floating point arithmetic. The Julia community being the Julia community there are some lengthy discussions on Discourse about this topic, here's one I found at random (no pun intended):

https://discourse.julialang.org/t/how-to-create-a-random-uniform-distribution-between-but-excluding-0-and-10/21908/2

答案2

得分: 1

如果你想创建Float16类型的随机值,至少有以下两种选项:

julia> 2 * rand(Float16) - 1
Float16(-0.952)

或者使用Distributions.jl

julia> d = Uniform{Float16}(-1, 1);

julia> rand(d)
0.3907014142060643

julia> rand(d) |> typeof
Float64
英文:

If you want to create random values of type Float16 you have at least these two options:

julia> 2*rand(Float16) - 1
Float16(-0.952)

Or using Distributions.jl

julia> d = Uniform{Float16}(-1, 1);

julia> rand(d)
0.3907014142060643

julia> rand(d) |> typeof
Float64

答案3

得分: 0

生成例如在-1和1之间的10个随机数,使用广播

julia> rand(10) .* rand((-1,1), 10)
10个Float64类型的元素:
  0.7832605719765726
  0.11799399505997143
  0.49379156313102823
 -0.4481589093858642
 -0.9394405800834137
  0.12834147407759333
 -0.9643503751879805
 -0.6624220140320877
  0.4220815095109952
  0.16949781166834121
英文:

Generating e.g. 10 random numbers between -1 and 1, using broadcasting

julia> rand(10) .* rand((-1,1), 10)
10-element Vector{Float64}:
  0.7832605719765726
  0.11799399505997143
  0.49379156313102823
 -0.4481589093858642
 -0.9394405800834137
  0.12834147407759333
 -0.9643503751879805
 -0.6624220140320877
  0.4220815095109952
  0.16949781166834121

huangapple
  • 本文由 发表于 2023年3月3日 22:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定