#' Set seed from any string
#'
#' Similar to set.seec() from base R, but will take any value
#'
#' @param x A single value (character, integer, numeric, etc.) to use as a seed
#' @param set Should the function also set.seed()?
#'
#' @export
#'
#' @examples
#' char2seed("1234abcd!@#$")
#' runif(3)
#' char2seed("1234abcd!@#$")
#' runif(3)
char2seed <- function(x, set=TRUE,...){
tmp <- c(0:9,0:25,0:25)
names(tmp) <- c(0:9,letters,LETTERS)
x <- gsub("[^0-9a-zA-Z]", "", as.character(x))
xsplit <- tmp[ strsplit(x,'')[[1]] ]
seed <- sum(rev( 7^(seq(along = xsplit)-1) ) * xsplit)
seed <- as.integer( seed %% (2^31-1) )
if(set){
set.seed(seed,...)
return(invisible(seed))
} else {
return(seed)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.