创建随机真/假一数组 [英] Create a array of random True/False

查看:174
本文介绍了创建随机真/假一数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我目前在做什么:

Here is what I am currently doing:

a = trues(100)
for i in 1:length(a)
   a[i] = rand()>0.5 ? true : false
end

有没有更好的(快)的解决方案?

Is there a better (faster) solution?

推荐答案

在朱莉娅0.4你可以写 bitrand(100)

In Julia 0.4 you can write bitrand(100):

julia> bitrand(100)
100-element BitArray{1}:
  true
  true
 false
 false
  true
     ⋮
  true
 false
  true
  true
  true

您可以在旧版本朱莉娅使用 COMPAT 包得到这个,或者你可以使用旧名称, randbool (行为相同,不同的名称)。 兰特西蒙的回答(BOOL,100)的作品,但它提供了一个阵{布尔} ,而不是 BitArray - 存储紧凑只使用每布尔有点布尔数组一个特殊的数据类型。

You can get this using the Compat package in older versions of Julia, or you can use the old name, randbool (same behavior, different name). Simon's answer of rand(Bool,100) works but it gives an Array{Bool} instead of a BitArray – a special data type that stores boolean arrays compactly using only a bit per boolean.

这篇关于创建随机真/假一数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆