SurfChu85

Subtractive PRNG Algorithm

Oct 11th, 2020 (edited)
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.11 KB | None | 0 0
  1. # referenced from http://rosettacode.org/wiki/Subtractive_generator
  2. # credits to Frizu (twitter: @KaidenFrizu) for fixing a roughly half-assed code I started without having enough sleep
  3. # imagine losing sleep over gacha drop rates... and you don't even play gacha
  4. # if being used, don't forget to input a seed to the function to reset rng sequence, else it will continue based from the prev. seed
  5. # the generation sequence is stored on temp55, pls don't alter or you'll lose sleep over it as well tnx
  6.  
  7. subtractive_gen <- function(seed = NULL) {
  8.     if(!is.null(seed)) {
  9.         temp <- c()
  10.         reorder_temp <- c()
  11.        
  12.         temp[1] <- seed
  13.         temp[2] <- 1
  14.        
  15.         for(i in 3:55) temp[i] <- (temp[i-2] - temp[i-1]) %% (10^9)
  16.         for(i in 1:55) reorder_temp[i] <- temp[((34*(i)) %% 55)+1]
  17.         for(i in 56:220) (reorder_temp[i] <- (reorder_temp[i-55] - reorder_temp[i-24]) %% (10^9))
  18.        
  19.         temp55 <- tail(reorder_temp, n = 55)
  20.     }
  21.    
  22.     temp55 <- append(temp55, (temp55[56-55] - temp55[56-24]) %% (10^9))
  23.     temp55 <<- tail(temp55, n = 55)
  24.     return(temp55[56])
  25. }
Add Comment
Please, Sign In to add comment