4 min read

Generate random and arbitrary data

Matrix of One (1) with specific dimension

ones(Int64, (2, 3))
2×3 Array{Int64,2}:
 1  1  1
 1  1  1
import numpy as np
print(np.ones((2, 3), dtype="int"))
[[1 1 1]
 [1 1 1]]
matrix(1, 2, 3)
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1

Matrix of same arbitrary element with specific dimension

fill(3.14, (2, 3))
2×3 Array{Float64,2}:
 3.14  3.14  3.14
 3.14  3.14  3.14
print(np.full((2, 3), 3.14))
[[3.14 3.14 3.14]
 [3.14 3.14 3.14]]
matrix(3.14, 2, 3)
     [,1] [,2] [,3]
[1,] 3.14 3.14 3.14
[2,] 3.14 3.14 3.14

Identity Matrix of specific dimension

using LinearAlgebra
diagm(0 => fill(1, 4))
4×4 Array{Int64,2}:
 1  0  0  0
 0  1  0  0
 0  0  1  0
 0  0  0  1
import numpy as np
print(np.identity(4))
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
diag(4)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1

Diagonal Matrix from a given vector

using LinearAlgebra
A = [2, 0, -1, 5];
diagm(0 => A)
4×4 Array{Int64,2}:
 2  0   0  0
 0  0   0  0
 0  0  -1  0
 0  0   0  5
import numpy as np
A = [2, 0, -1, 5]
print(np.diag(A))
[[ 2  0  0  0]
 [ 0  0  0  0]
 [ 0  0 -1  0]
 [ 0  0  0  5]]
A = c(2, 0, -1, 5)
diag(A)
     [,1] [,2] [,3] [,4]
[1,]    2    0    0    0
[2,]    0    0    0    0
[3,]    0    0   -1    0
[4,]    0    0    0    5

Random numbers from a distribution

Uniform Distribution

using Distributions
dist = Uniform();
permutedims(rand(dist, 5))
1×5 Array{Float64,2}:
 0.984751  0.670535  0.185211  0.0369733  0.508252

Normal Distribution

dist = Normal();
permutedims(rand(dist, 5))
1×5 Array{Float64,2}:
 -0.558982  0.0790834  -0.66041  -0.921208  1.16947
## Alternatively
permutedims(randn(5))
1×5 Array{Float64,2}:
 -0.981928  0.934959  0.322153  0.515093  0.346476

Binomial Distribution

dist = Binomial();
permutedims(rand(dist, 5))
1×5 Array{Int64,2}:
 0  0  1  1  0
import numpy as np

Uniform Distribution

print(np.random.uniform(size=5))
[0.19356198 0.54105532 0.64376911 0.58289997 0.24478789]

Normal Distribution

print(np.random.normal(size=5))
[-0.63691875  0.42242821 -0.60399815  0.82398874 -0.85590315]

Binomial Distribution

print(np.random.binomial(1, 0.5, size=5))
[1 1 0 0 1]

Uniform Distribution

runif(n = 5)
[1] 0.10756923 0.52867896 0.80806652 0.92465158 0.08007269

Normal Distribution

rnorm(n = 5)
[1] -0.6240483  1.5799203 -1.4466223 -1.3213345 -0.5432923

Binomial Distribution

rbinom(n = 5, size = 1, prob = 0.5)
[1] 1 0 1 1 1

Random sampling

## Sampling Text

using StatsBase
text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.";
text_vec = split(text, " ");
sample(text_vec, 5; replace = false)
5-element Array{SubString{String},1}:
 "printing" 
 "dummy"    
 "remaining"
 "Lorem"    
 "dummy"    

## Sampling Numbers

permutedims(sample(0:20, 5; replace = false))
1×5 Array{Int64,2}:
 2  9  1  18  0
## Sampling Text
import numpy as np
text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
text_vec = text.split(" ")
print(np.random.choice(text_vec, replace=False, size=5))
## Sampling Number
['remaining' 'but' 'type' 'dummy' 'It']
print(np.random.choice(range(20), replace=False, size=5))
[19  9 17  1  0]
## Sampling Text

text <- "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
text_vec <- unlist(strsplit(text, " "))
sample(text_vec, size = 5, replace = FALSE)
[1] "five"        "typesetting" "printing"    "Lorem"       "to"         
## Sampling Number
sample(0:20, 5, replace = FALSE)
[1]  4 10  7  1 15