random - Julia,生成四个 1 :3, 之间的 Int8 整数数组,我需要一个包吗?

标签 random integer julia

Ubuntu 上的 Julia 1.5.3。我试图简单地生成一个由四个 Int8 整数组成的数组,其范围为 1:3。尝试了一堆不同的语法但只是出现错误。我无法控制类型(Int8)和数字或返回(4)。谢谢。 J

rand(1:3, 4) # works
rand(Int8, 4) # works
rand(Int8, 1:3, 4) #error
rand(Int8, 4, 1:3) #error
rand(Int8(1:3), 3) #error

最佳答案

最优雅、最高效的是:

rand(UnitRange{Int8}(1,3), 4)

编辑

Benoit Pasquier 指出了Int8(1):Int8(3),他确实是对的。

这会产生更高效的汇编代码,因为编译器可以避免类型检查(当然只有当 13 是具体值时)。

以下是查找方法:

julia> @code_native (:)(Int8(1),Int8(3))
        .text
; ┌ @ range.jl:5 within `Colon'
        pushq   %rbp
        movq    %rsp, %rbp
        movl    %ecx, %eax
; │┌ @ range.jl:287 within `UnitRange'
; ││┌ @ range.jl:292 within `unitrange_last'
; │││┌ @ int.jl:86 within `-'
        leal    -1(%rax), %r8d
; │││└
; │││┌ @ operators.jl:352 within `>='
; ││││┌ @ int.jl:442 within `<='
        cmpb    %dl, %al
; │││└└
        movzbl  %dl, %ecx
        movzbl  %r8b, %edx
        cmovlel %ecx, %edx
; │└└
        popq    %rbp
        retq
        nopl    (%rax,%rax)
; └

比较:

julia> @code_native UnitRange{Int8}(1,3)                                   
        .text                                                              
; ┌ @ range.jl:287 within `UnitRange'                                      
        pushq   %rbp                                                       
        movq    %rsp, %rbp                                                 
        subq    $32, %rsp                                                  
        movq    %rcx, %rax                                                 
; │┌ @ number.jl:7 within `convert'                                        
; ││┌ @ boot.jl:749 within `Int8'                                          
; │││┌ @ boot.jl:639 within `toInt8'                                       
; ││││┌ @ boot.jl:624 within `checked_trunc_sint'                          
        movsbq  %al, %rcx                                                  
        cmpq    %rax, %rcx                                                 
        jne     L49                                                        
; │└└└└                                                                    
; │ @ boot.jl within `UnitRange'                                           
        movq    %rdx, %r8                                                  
; │ @ range.jl:287 within `UnitRange'                                      
; │┌ @ range.jl:292 within `unitrange_last'                                
; ││┌ @ int.jl:86 within `-'                                               
        leaq    -1(%rax), %rdx                                             
; ││└                                                                      
; ││┌ @ operators.jl:352 within `>='                                       
; │││┌ @ int.jl:442 within `<='                                            
        cmpq    %r8, %rax                                                  
; ││└└                                                                     
        cmovleq %r8, %rdx                                                  
; │└                                                                       
; │┌ @ number.jl:7 within `convert'                                        
; ││┌ @ boot.jl:749 within `Int8'                                          
; │││┌ @ boot.jl:639 within `toInt8'                                       
; ││││┌ @ boot.jl:624 within `checked_trunc_sint'                          
        movsbq  %dl, %rcx                                                  
        cmpq    %rdx, %rcx                                                 
        jne     L72                                                        
; │└└└└                                                                    
        addq    $32, %rsp                                                  
        popq    %rbp                                                       
        retq                                                               
; │┌ @ number.jl:7 within `convert'                                        
; ││┌ @ boot.jl:749 within `Int8'                                          
; │││┌ @ boot.jl:639 within `toInt8'                                       
; ││││┌ @ boot.jl:624 within `checked_trunc_sint'                          
L49:                                                                       
        movabsq $throw_inexacterror, %r8                                   
        movl    $243577088, %ecx                # imm = 0xE84B100          
        movq    %rax, %rdx                                                 
        callq   *%r8                                                       
        ud2                                                                
L72:                                                                       
        movabsq $throw_inexacterror, %rax                                  
        movl    $243577088, %ecx                # imm = 0xE84B100          
        callq   *%rax                                                      
        ud2                                                                
        nopl    (%rax,%rax)                                                
; └└└└└                                                                    
                                                                           

关于random - Julia,生成四个 1 :3, 之间的 Int8 整数数组,我需要一个包吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65732938/

相关文章:

c# - 在 C# 中生成随机配对图像

python - 使用 Pandas 的赋值和 Lambda 函数向每行添加 Rand

c - 如何在C中从这种类型的字符串中获取数字

python - 将数字拆分为所有可能的数字组,保持原始顺序

mysql - 色谱柱的最佳用途是什么?

matlab - 如何从 Julia 中的字典创建结构或类型?

java - Alias 方法的开源实现

type-conversion - Julia 函数内部没有自动类型转换

math - 如何在 Julia 中调用 LAPACK 代码 (cpbtrf)

ruby - 为什么我要为 Array#shuffle 使用自定义 RNG?