Skip to contents

This function is a wrapper around rbinom to generate bit vectors. The matrix and array version are creating by row.

Usage

rbits(size, p = 0.5, ...)

Arguments

size

The size of the vector. Can be a list of dimensions to create a vector, matrix or array. If a vector larger than 3 is provided, each value is treated as the probability of obtaining 1 and a vector of bits is generated using a binomial distribution.

p

Probability of obtaining a 1. By default it's 0.5.

...

Extra arguments passed to matrix or array

Value

The generated bit vector

Examples

## Unbiased probabilities
rbits(10)
#>  [1] 1 1 1 0 0 0 1 0 0 0

## Biased probabilities
rbits(10, p = 0.8)
#>  [1] 1 1 1 0 1 1 1 1 1 1

## Matrix of bits
rbits(c(3, 4))
#>      [,1] [,2] [,3] [,4]
#> [1,]    0    0    0    0
#> [2,]    1    1    1    1
#> [3,]    1    0    0    0

## 3D Array of bits
rbits(c(3, 4, 2))
#> , , 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    0    1    0    0
#> [2,]    1    1    0    0
#> [3,]    0    0    0    1
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    0    0    1    1
#> [2,]    0    1    1    1
#> [3,]    1    1    0    1
#> 

## Individual probabilities
rbits(c(3, 3), runif(9, max = 0.5))
#>      [,1] [,2] [,3]
#> [1,]    0    0    0
#> [2,]    1    0    1
#> [3,]    0    1    0