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?
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?
In Julia 0.4 you can write
bitrand(100)
: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 ofrand(Bool,100)
works but it gives anArray{Bool}
instead of aBitArray
– a special data type that stores boolean arrays compactly using only a bit per boolean.I haven't benchmarked it but the fastest option seems likely to be:
... see the bottom of the Julia documentation page on Multi-dimensional Arrays.