Efficient Conversion of a Large Integer List to Boolean List

I am working on a problem where I need to generate a list of random Boolean values. My current approach is to first generate a list of random integers (0 or 1), and then convert this list to a Boolean list. However, I am struggling to find an efficient way to perform this conversion.

My current method uses the Map function, but it's quite slow, especially when dealing with large lists. Here's my code:

Module[{v = RandomInteger[{0, 1}, 10000000]},
AbsoluteTiming[# != 0 & /@ v]]


This operation takes around 1.96 seconds to run on my machine, which is too slow for my needs.

I am looking for a more efficient way to convert a large list of integers to a Boolean list. Any suggestions or improvements would be greatly appreciated.

• AbsoluteTiming[v = RandomChoice[{False, True}, 10000000]]
– Syed
Aug 15, 2023 at 1:54
• @Syed Thank you! It's indeed much faster, takes 0.165785 seconds, while I find RandomInteger is even faster, takes 0.0229642 seconds. Aug 15, 2023 at 2:06
• I left it as a comment as your first sentence says: I need to generate a list of random Boolean values and it changes to I am looking for a more efficient way to convert a large list of integers to a Boolean list at the end.
– Syed
Aug 15, 2023 at 2:15
• If you are working with massive numbers of Booleans, maybe it'd be faster to change your code to take {0,1} instead of {False,True} since AbsoluteTiming[v = RandomInteger[{0, 1}, 10000000];] is about 2 times faster than AbsoluteTiming[v = RandomChoice[{True, False}, 10000000];] This may be irrelevant to your application though, as the main bottleneck in your code could be somewhere else
– ydd
Aug 15, 2023 at 2:19
• v /. {0 -> False, 1 -> True} should be faster than your approach, if you must start from numerical values. Direct generation of an array of Boolean values with RandomChoice will still be faster though. Aug 15, 2023 at 8:55