R语言生成随机数3维图(上)

最近在Rbloggers上看到了一篇很有趣的博文,特转载如下。

1
library(rgl)

rnorm()函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
set.seed(1234)
r <- rnorm(1000)
x <- r[seq(1, length(r), 3)]
str(x)
## num [1:334] -1.207 -2.346 -0.575 -0.89 -0.776 ...
y <- r[seq(2, length(r), 3)]
str(y)
## num [1:333] 0.2774 0.4291 -0.5466 -0.4772 0.0645 ...
z <- r[seq(3, length(r), 3)]
str(z)
## num [1:333] 1.084 0.506 -0.564 -0.998 0.959 ...
lim <- function(x) {
c(-max(abs(x)), max(abs(x))) * 1.1
}
1
2
3
4
5
6
7
8
rgl.open()
rgl.bg(color = "white")
rgl.spheres(x, y, z, r = 0.5, color = "lightblue")
rgl.lines(lim(x), c(0, 0), c(0, 0), color = "black", lw = 1.5)
rgl.lines(c(0, 0), lim(y), c(0, 0), color = "red", lw = 1.5)
rgl.lines(c(0, 0), c(0, 0), lim(z), color = "blue", lw = 1.5)
grid3d(c("x", "y", "z"))
rglwidget()

rbinom()函数

1
r <- rbinom(1000, 100, 0.3)
1
2
x <- r[seq(1, length(r), 3)]
str(x)
1
## int [1:334] 29 24 24 40 27 24 24 30 30 29 ...
1
2
y <- r[seq(2, length(r), 3)]
str(y)
1
## int [1:333] 31 37 29 32 26 29 30 19 27 29 ...
1
2
z <- r[seq(3, length(r), 3)]
str(z)
1
## int [1:333] 25 31 30 29 36 33 26 31 24 33 ...
1
2
3
lim <- function(x) {
c(-max(abs(x)), max(abs(x))) * 1.1
}
1
2
3
4
5
6
7
8
rgl.open()
rgl.bg(color = "white")
rgl.spheres(x, y, z, r = 0.5, color = "lightblue")
rgl.lines(lim(x), c(0, 0), c(0, 0), color = "black", lw = 1.5)
rgl.lines(c(0, 0), lim(y), c(0, 0), color = "red", lw = 1.5)
rgl.lines(c(0, 0), c(0, 0), lim(z), color = "blue", lw = 1.5)
grid3d(c("x", "y", "z"))
rglwidget()

rpois()函数

1
r <- rpois(1000, 10) # 可以尝试不同的自由度
1
2
x <- r[seq(1, length(r), 3)]
str(x)
1
## int [1:334] 7 14 12 9 12 6 8 13 11 6 ...
1
2
y <- r[seq(2, length(r), 3)]
str(y)
1
## int [1:333] 10 10 12 10 7 9 8 16 7 8 ...
1
2
z <- r[seq(3, length(r), 3)]
str(z)
1
## int [1:333] 9 12 10 12 8 8 7 7 9 13 ...
1
2
3
lim <- function(x) {
c(-max(abs(x)), max(abs(x))) * 1.1
}
1
2
3
4
5
6
7
8
rgl.open()
rgl.bg(color = "white")
rgl.spheres(x, y, z, r = 0.5, color = "lightblue")
rgl.lines(lim(x), c(0, 0), c(0, 0), color = "black", lw = 1.5)
rgl.lines(c(0, 0), lim(y), c(0, 0), color = "red", lw = 1.5)
rgl.lines(c(0, 0), c(0, 0), lim(z), color = "blue", lw = 1.5)
grid3d(c("x", "y", "z"))
rglwidget()

runif()函数

1
r <- runif(9000)
1
2
x <- r[seq(1, length(r), 3)]
str(x)
1
## num [1:3000] 0.0967 0.6981 0.4379 0.6292 0.4695 ...
1
2
y <- r[seq(2, length(r), 3)]
str(y)
1
## num [1:3000] 0.816 0.891 0.825 0.288 0.212 ...
1
2
z <- r[seq(3, length(r), 3)]
str(z)
1
## num [1:3000] 0.133 0.288 0.7 0.469 0.425 ...
1
2
3
lim <- function(x) {
c(-max(abs(x)), max(abs(x))) * 1.1
}
1
2
3
4
5
6
7
8
rgl.open()
rgl.bg(color = "white")
rgl.spheres(x, y, z, r = 0.5, color = "lightblue")
rgl.lines(lim(x), c(0, 0), c(0, 0), color = "black", lw = 1.5)
rgl.lines(c(0, 0), lim(y), c(0, 0), color = "red", lw = 1.5)
rgl.lines(c(0, 0), c(0, 0), lim(z), color = "blue", lw = 1.5)
grid3d(c("x", "y", "z"))
rglwidget()

rlogis()函数

1
r <- rlogis(1000)
1
2
x <- r[seq(1, length(r), 3)]
str(x)
1
## num [1:334] -2.496 2.9 -2.888 -0.472 -2.567 ...
1
2
y <- r[seq(2, length(r), 3)]
str(y)
1
## num [1:333] -1.705 6.009 3.371 1.448 -0.184 ...
1
2
z <- r[seq(3, length(r), 3)]
str(z)
1
## num [1:333] -2.592 2.018 -1.501 -2.163 -0.173 ...
1
2
3
lim <- function(x) {
c(-max(abs(x)), max(abs(x))) * 1.1
}
1
2
3
4
5
6
7
8
rgl.open()
rgl.bg(color = "white")
rgl.spheres(x, y, z, r = 0.5, color = "lightblue")
rgl.lines(lim(x), c(0, 0), c(0, 0), color = "black", lw = 1.5)
rgl.lines(c(0, 0), lim(y), c(0, 0), color = "red", lw = 1.5)
rgl.lines(c(0, 0), c(0, 0), lim(z), color = "blue", lw = 1.5)
grid3d(c("x", "y", "z"))
rglwidget()

rbeta()函数

1
r <- rbeta(1000, 1, 2)
1
2
x <- r[seq(1, length(r), 3)]
str(x)
1
## num [1:334] 0.839 0.166 0.376 0.158 0.316 ...
1
2
y <- r[seq(2, length(r), 3)]
str(y)
1
## num [1:333] 0.65435 0.26735 0.0932 0.12623 0.00374 ...
1
2
z <- r[seq(3, length(r), 3)]
str(z)
1
## num [1:333] 0.4725 0.6237 0.823 0.6512 0.0565 ...
1
2
3
lim <- function(x) {
c(-max(abs(x)), max(abs(x))) * 1.1
}
1
2
3
4
5
6
7
8
rgl.open()
rgl.bg(color = "white")
rgl.spheres(x, y, z, r = 0.5, color = "lightblue")
rgl.lines(lim(x), c(0, 0), c(0, 0), color = "black", lw = 1.5)
rgl.lines(c(0, 0), lim(y), c(0, 0), color = "red", lw = 1.5)
rgl.lines(c(0, 0), c(0, 0), lim(z), color = "blue", lw = 1.5)
grid3d(c("x", "y", "z"))
rglwidget()

rchisq()函数

1
r <- rchisq(1000, 15) # 可以尝试不同的自由度
1
2
x <- r[seq(1, length(r), 3)]
str(x)
1
## num [1:334] 13.41 7.21 13.96 15.81 11.39 ...
1
2
y <- r[seq(2, length(r), 3)]
str(y)
1
## num [1:333] 13.3 19.36 6.48 18.32 18.05 ...
1
2
z <- r[seq(3, length(r), 3)]
str(z)
1
## num [1:333] 28.2 19.6 20.6 17.4 9.5 ...
1
2
3
lim <- function(x) {
c(-max(abs(x)), max(abs(x))) * 1.1
}
1
2
3
4
5
6
7
8
rgl.open()
rgl.bg(color = "white")
rgl.spheres(x, y, z, r = 0.5, color = "lightblue")
rgl.lines(lim(x), c(0, 0), c(0, 0), color = "black", lw = 1.5)
rgl.lines(c(0, 0), lim(y), c(0, 0), color = "red", lw = 1.5)
rgl.lines(c(0, 0), c(0, 0), lim(z), color = "blue", lw = 1.5)
grid3d(c("x", "y", "z"))
rglwidget()

原文地址:How Good is That Random Number Generator?