我想在R和Julia中生成相同的随机数.默认情况下,这两种语言似乎都使用Mersenne-Twister库,但在Julia 1.0.0中:
julia> using Random
julia> Random.seed!(3)
julia> rand()
0.8116984049958615
Run Code Online (Sandbox Code Playgroud)
生产0.811...,而在R:
set.seed(3)
runif(1)
Run Code Online (Sandbox Code Playgroud)
生产0.168.
有任何想法吗?
我感兴趣的用例:通过比较输出与R中等效库的输出,测试需要随机数生成(例如统计引导)的新Julia代码
我想在Julia中将整数转换为字符串.
当我尝试:
a = 9500
b = convert(String,a)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
ERROR: LoadError: MethodError: Cannot `convert` an object of type Int64 to an object of type String
This may have arisen from a call to the constructor String(...),
since type constructors fall back to convert methods.
in include_from_node1(::String) at ./loading.jl:488
in process_options(::Base.JLOptions) at ./client.jl:265
in _start() at ./client.jl:321
while loading ..., in expression starting on line 16
Run Code Online (Sandbox Code Playgroud)
我不确定为什么Int64不能转换为字符串.
a例如a = UInt64(9500),我尝试定义为不同的类型,但得到类似的错误.
我知道这是非常基本的,并试图在这里寻找正确的方法,但无法弄明白.
在julia 1.0.0中,我得到以下for循环作用域行为:
julia> counts = 0
0
julia> for i in 1:10
counts += 1
end
ERROR: UndefVarError: counts not defined
Run Code Online (Sandbox Code Playgroud)
我发现解决方案是在循环中创建counts变量.globalfor
julia> for i in 1:10
global counts += 1
end
julia> counts
10
Run Code Online (Sandbox Code Playgroud)
然而,作为朱莉娅的新手,这种行为几乎让我退出了语言,因为它似乎与其他语言有很大不同.
现在我看到了上面的解决方案,我想知道这对于开始julia用户是否直观.这对我来说并不直观,尽管我终于能够在很长一段时间后解决它.
这是令人困惑的部分.我认为在初始化时创建变量global会解决问题.它不是:
julia> global c = 0
julia> for i in 1:10
c += 1
end
ERROR: UndefVarError: c not defined
Run Code Online (Sandbox Code Playgroud)
c上面的全局范围会流入for循环似乎很自然,但for循环的第一次初始化c显然会创建一个不同的for循环局部c.
这对经验丰富的julia开发者有意义吗?
我看到了这个Stackoverflow代码=>,但是当我在 Julia 1.0.0 在线帮助中搜索“=>”时,我得到零点击。
replace!(x, 0=>4) # The last expression is the focus of this question.
Run Code Online (Sandbox Code Playgroud)
在 REPL 帮助中,我得到:
help?> =>
search: =>
Pair(x, y)
x => y
Run Code Online (Sandbox Code Playgroud)
构造一个类型为 Pair 的对象
Pair{typeof(x), typeof(y)}。元素存储在字段 first 和 second 中。它们也可以通过迭代访问。另见:字典
例子 ??????????
julia> p = "foo" => 7
"foo" => 7
julia> typeof(p)
Pair{String,Int64}
julia> p.first
"foo"
julia> for x in p
println(x)
end
foo
7
Run Code Online (Sandbox Code Playgroud)
在=>做什么replace!(x, 0=>4)?它是创建一对,用四个替换所有零,还是什么?为什么我在 Julia 1.0.0 在线文档中似乎没有找到它?
编辑
添加了代码以帮助我理解下面@Bill …
此堆栈链接显示如何返回数组中的值booleans。missing
例如:
julia> A = [1, 2, 3, missing, 4, 5, 6, missing, 7, 8, missing, 9, 10]
13-element Array{Union{Missing, Int64},1}:
1
2
3
missing
4
5
6
missing
7
8
missing
9
10
julia> ismissing.(A)
13-element BitArray{1}:
false
false
false
true
false
false
false
true
false
false
true
false
false
Run Code Online (Sandbox Code Playgroud)
如何返回他们的索引?
工程符号与科学符号的不同之处在于:
指数总是3的倍数,和
小数点左侧的数字按比例调整为1到999.
我的用例要求在小数点右侧指定0到13位数.默认值为4.
const Avogadro = 6.022140857e23
str = eng_notation(Avogadro, digits=0)
# str = "602E+21"
str = eng_notation(Avogadro, digits=1)
# str = "602.2E+21"
# Default 4 digits to right of decimal point.
str = eng_notation(Avogadro)
# str = "602.2141E+21"
str = eng_notation(Avogadro, digits=10)
# str = "602.2140857000E+21"
# Negative and fractional numbers should also work.
str = eng_notation(-0.01234567, digits=7)
# str = "-12.4567000E-03"
Run Code Online (Sandbox Code Playgroud)
有什么建议?
如果 an 的任何元素丢失,Statistics 包mean函数将返回。missingarray
julia> using Statistics
julia> mean([1 2 3 4 5] )
3.0
julia> mean([1 2 missing 4 5] ) # Note missing value
missing
Run Code Online (Sandbox Code Playgroud)
如何获得非缺失值的平均值?
我的 Julia REPL 帮助为 LOAD_PATH 提供了以下内容:
help?> LOAD_PATH
search: LOAD_PATH
LOAD_PATH
An array of paths for using and import statements to consdier as project environments or package directories when
loading code. See Code Loading.
Run Code Online (Sandbox Code Playgroud)
这是我在提示符下的 LOAD_PATH 输出:
julia> LOAD_PATH # What is the output below?
3-element Array{String,1}:
"@"
"@v#.#"
"@stdlib"
Run Code Online (Sandbox Code Playgroud)
上面显示的 LOAD_PATH 输出看起来很奇怪。
有什么建议么?
在Python中,很容易创建一个字符串n:
>>> '=' * 40
'========================================'
Run Code Online (Sandbox Code Playgroud)
但是,在Julia中,上述方法不起作用.什么是Julia相当于上面的Python代码?