rmuc8
rmuc8

Reputation: 2989

Generating a (random) sequence of multiple characters in R

  1. I can create a sequence of single letters using

    LETTERS[seq( from = 1, to = 10 )]
    letters[seq( from = 1, to = 10 )]
    
  2. I can create a random string of various length, using the random package

    library(random)
    string <- randomStrings(n=10, len=5, digits=TRUE, upperalpha=TRUE,
                      loweralpha=TRUE, unique=TRUE, check=TRUE)
    

Unfortunately, I cannot use the set.seed function for example 2.

Is there a way to create the same (random) combination of (unique) strings everytime one runs a R-file?

My result would look like this (with the same result everytime I run the function):

       V1     
  [1,] "k7QET"
  [2,] "CLlWm"
  [3,] "yPuwh"
  [4,] "JJqEX"
  [5,] "38soF"
  [6,] "xkozk"
  [7,] "uaiOW"
  [8,] "tZcrW"
  [9,] "8K4Cc"
 [10,] "RAhuU"

Upvotes: 8

Views: 16796

Answers (3)

ypa y yhm
ypa y yhm

Reputation: 219

Make some random number vector (in a range) and give them to intToUtf8.

This solution could generates random characters, and you don't need to import any libraries. (well I just need a magrittr to pretty my code ...)


e.g.

You can use this to see a code number in UTF-8 means which character:

#' @license mit
#' 

#' @examples 
#' 
#' `9999 %>% seq %>% utf8_see`
#' `19894:19999 %>% utf8_see`
#' ...
#' 
#' `c(65:90,97:122) %>% utf8_see` is A-Z and a-z
#' `48:57 %>% utf8_see` is 0-9
#' `9200:9203 %>% utf8_see` is some clock emoji
#' `10240:10495 %>% utf8_see` is the brailles
#' `c(768:879,1155:1158,1160:1161) %>% utf8_see` is some zero width char
#' `c(688:705,710:740,748:767,1159) %>% utf8_see` is some super/sub scripts
#' `c(94,581,652,654,710,923,955) %>% utf8_see` is some lambda like char
#' ...
#' 
utf8_see = function (n) `names<-`(n,n) |> 
    vapply (intToUtf8, FUN.VALUE="character") ;

You can just execute the codes in the examples by yourself.

Then, you can just use sample(n) to pick some number in it, and trans them into character with intToUtf8. Such as:

c(65:90,97:122) %>% sample(5) %>% intToUtf8

This will give you a 5 length string with random letters, or you can make this many time i.e.

7 %>% seq %>% vapply(\ (x) c(65:90,97:122) %>% sample(5) %>% intToUtf8, FUN.VALUE="character")

So we can also make a tool:

library(magrittr)

#' @license agpl-3.0
#' @author y.ypa.yhm
#' 
rand.chars = \ (char.set) \ (len, times) times %>% seq %>% 
    vapply(\ (x) char.set %>% sample(len) %>% intToUtf8, FUN.VALUE="character")

`%a2z.randchars%` = c(65:90,97:122) %>% rand.chars
`%braille.randchars%` = 10240:10495 %>% rand.chars
`%num.randchars%` = 48:57 %>% rand.chars
`%lambda.randchars%` = c(94,581,652,654,710,923,955) %>% rand.chars

Then use like:

  • 5 %a2z.randchars% 10
  • 3 %braille.randchars% 4
  • 9 %num.randchars% 3
  • 4 %lambda.randchars% 3

This is my generations:

> 5 %a2z.randchars% 10
 [1] "OzQIv" "wQume" "SWaQL" "nBmSE" "gjSvR" "lTMfe" "kJGqt"
 [8] "FcGnf" "jsEUw" "AhFRf"
> 3 %braille.randchars% 4
[1] "⢿⠖⡵" "⣒⠥⣑" "⣮⠨⡆" "⠘⠗⡇"
> 9 %num.randchars% 3
[1] "139475082" "286974310" "389520461"
> 4 %lambda.randchars% 3
[1] "ˆʌɅ^"  "Ʌ^λˆ" "ʎ^λɅ"

All codes tested at the Webr Playground and R Studio, here is a demo on Shinylive.

Enjoy it. 🎉

Upvotes: 0

Derek Powell
Derek Powell

Reputation: 523

An alternate solution in base R:

set.seed(1)
result <- rawToChar(as.raw(sample(c(65:90,97:122), 5, replace=T)))

Inspired by this answer to another post: https://stackoverflow.com/a/4370074/8297546

Upvotes: 9

Rich Scriven
Rich Scriven

Reputation: 99391

In a file, say test.R, add the following

set.seed(1)
stringi::stri_rand_strings(10, 5)

Then it's reproducible every time.

replicate(5, source("test.R", verbose = FALSE)$value)
#       [,1]    [,2]    [,3]    [,4]    [,5]    
#  [1,] "GNZuC" "GNZuC" "GNZuC" "GNZuC" "GNZuC" 
#  [2,] "twed3" "twed3" "twed3" "twed3" "twed3"
#  [3,] "CAgNl" "CAgNl" "CAgNl" "CAgNl" "CAgNl"
#  [4,] "UizNm" "UizNm" "UizNm" "UizNm" "UizNm" 
#  [5,] "vDe7G" "vDe7G" "vDe7G" "vDe7G" "vDe7G"
#  [6,] "N0NrL" "N0NrL" "N0NrL" "N0NrL" "N0NrL"
#  [7,] "TbUBp" "TbUBp" "TbUBp" "TbUBp" "TbUBp"
#  [8,] "fn6iP" "fn6iP" "fn6iP" "fn6iP" "fn6iP" 
#  [9,] "oemYW" "oemYW" "oemYW" "oemYW" "oemYW"
# [10,] "m1Tjg" "m1Tjg" "m1Tjg" "m1Tjg" "m1Tjg"

As an alternative to source(), you could use parse() there.

replicate(5, eval(parse("test.R")))

Upvotes: 12

Related Questions