Skip to content

Commit 74153c7

Browse files
committed
Avoid advancing RNG when creating agg devices
1 parent f606c63 commit 74153c7

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

R/agg_dev.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ agg_capture <- function(
427427
}
428428
dim <- get_dims(width, height, units, res)
429429
background <- if (missing(bg)) background else bg
430-
name <- paste0('agg_capture_', sample(.Machine$integer.max, 1))
430+
name <- basename(tempfile('agg_capture_'))
431431
.Call(
432432
"agg_capture_c",
433433
name,
@@ -504,7 +504,7 @@ agg_record <- function(
504504
}
505505
dim <- get_dims(width, height, units, res)
506506
background <- if (missing(bg)) background else bg
507-
name <- paste0('agg_record_', sample(.Machine$integer.max, 1))
507+
name <- basename(tempfile('agg_record_'))
508508
.Call(
509509
"agg_record_c",
510510
name,

tests/testthat/test-rng-state.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
test_that("agg devices do not advance global RNG state on creation", {
2+
3+
set.seed(123)
4+
dev <- agg_capture()
5+
handle <- dev()
6+
dev.off()
7+
a1 <- runif(1)
8+
b1 <- .Random.seed
9+
10+
set.seed(123)
11+
a2 <- runif(1)
12+
b2 <- .Random.seed
13+
14+
expect_equal(a1, a2)
15+
expect_equal(b1, b2)
16+
})
17+
18+
test_that("agg_record does not advance global RNG state on creation", {
19+
# with ragg
20+
set.seed(456)
21+
agg_record()
22+
dev.off()
23+
a1 <- runif(1)
24+
b1 <- .Random.seed
25+
26+
set.seed(456)
27+
a2 <- runif(1)
28+
b2 <- .Random.seed
29+
30+
expect_equal(a1, a2)
31+
expect_equal(b1, b2)
32+
})

0 commit comments

Comments
 (0)