randomcoloR icon indicating copy to clipboard operation
randomcoloR copied to clipboard

Set seed to fully control being repeatable

Open Puriney opened this issue 5 years ago • 0 comments

Problem

  1. Both the random colors and discrete color will not be repeatable in a new R session, as described in issue #13.
  2. Setting seed as issue #3 did cannot make the randomColor's results repeatable. This is because internally the randomColor.js has its own seed number and should be set accordingly.

After this PR is merged, these will happen:

  1. Both the randomColor and distinctColorPalette will generate repeatable colors.
  2. The seed number used by those two functions will NOT interrupt the randomness in the global environment.

Run the added test code in readme_demo/test_repeatable.R to feel this PR

library(scales)
library(randomcoloR)

c1 <- distinctColorPalette(k = 20, seed=12)
c2 <- randomColor(count=20, hue = 'red', luminosity = 'light', seed=2020)

message('Distinct colors are repeatable:')
print(c1)
message('The seed number in randomcolorR does not interrupt the global randomness')
print(rnorm(10))
message('Random colors are repeatable:')
print(c2)
message('The seed number in randomcolorR does not interrupt the global randomness')
print(rnorm(10))

pdf("~/repeatable.pdf", width = 7, onefile = T)
plot(1)
scales::show_col(c1)
scales::show_col(c2)
dev.off()

Result:

Distinct colors are repeatable:
 [1] "#D73AE1" "#E0BCD9" "#7ACDE5" "#DF67CD" "#D3E04D" "#D19E87" "#8249E2"
 [8] "#E3A244" "#7C74E1" "#AFC1DA" "#D8DA91" "#6EE9D6" "#D4E4D1" "#8A5598"
[15] "#D498DE" "#82A0E3" "#E1687B" "#709E86" "#88E299" "#7BE953"
The seed number in randomcolorR does not interrupt the global randomness
 [1]  0.6725051  0.1504643  0.8634863 -0.2982528  1.8013768 -1.2937986
 [7] -0.4185201  0.7720086 -0.1350407 -1.4384490
Random colors are repeatable:
 [1] "#f7b5ad" "#fccdc2" "#f79479" "#fca78d" "#f298bf" "#f7c0ad" "#fcc4dc"
 [8] "#f97caa" "#ef88a7" "#f49cbb" "#f9b1c8" "#fcc4d4" "#fc7e9d" "#ef8b9a"
[15] "#f49fb0" "#f9b3bf" "#fcc7ce" "#fc808d" "#f28c93" "#f7a6a3"
The seed number in randomcolorR does not interrupt the global randomness
 [1]  1.7064048  0.1825268 -0.2825259 -0.4292656 -0.5898879  0.3666581
 [7]  0.7370429 -1.0799768  1.6670583 -0.3806285
null device
          1

Puriney avatar May 03 '20 16:05 Puriney