why random is not random?

Why dplyr::slice_sample(n=3,replace=FALSE) gives ma the same result across script sessions? And how to get always different results?

I did not found slice_sample act random across R-sessions (no true random numbers, initial seeded on 1)

I get a different result from that snippet, but runs well at the end of a pipe passing a data frame.

dplyr::slice_sample(n=3,replace=FALSE)
#> Error in UseMethod("slice_sample"): no applicable method for 'slice_sample' applied to an object of class "c('double', 'numeric')"
mtcars |> dplyr::slice_sample(n = 5)
#>                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
#> Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#> Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#> Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
#> Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
data.frame(stuff = 1:100) |> dplyr::slice_sample(n = 3)
#>   stuff
#> 1    64
#> 2    85
#> 3    41
data.frame(stuff = 1:100) |> dplyr::slice_sample(n = 3)
#>   stuff
#> 1    96
#> 2    61
#> 3    22

Created on 2023-01-31 by the reprex package (v2.0.1)

Did it this way, giving me true random samples across R-sessions...

myGrid <- expand.grid(
    ntrees            = myNtrees
    ,max_depth        = myMaxDepth
    ,stringsAsFactors = FALSE
    ,KEEP.OUT.ATTRS   = FALSE
)
  
origNrow <- nrow(myGrid)    
  
# custom random grid search
gridRows <- 3 # random grid rows anz    
dqRNGkind("Xoroshiro128+")
idx      <- nrow(myGrid)
randNo   <- as.vector(as.numeric(dqrng::dqrunif(gridRows,min=1,max=idx)))
randNo   <- round(randNo,0)

myGrid   <- myGrid %>% 
    dplyr::mutate(id=dplyr::row_number()) %>%
    dplyr::filter(id %in% randNo) %>%
    dplyr::select(-any_of(c('id')))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.