R: Generate random string name

18Dec08

I want to produce a lot of .png files and need to name them. One possibility would be to let R automatically name them e.g. when used in a for loop.

In my case I need the name for each graphic as an object as I want to address it via a link. Thus I decided to write a small function that produces a random string made up of small and capital letters as well as numbers. The default setting is to generate one string with 12 characters length.

###############################################################
#
# MHmakeRandomString(n, length)
# function generates a random string random string of the
# length (length), made up of numbers, small and capital letters

MHmakeRandomString <- function(n=1, lenght=12)
{
    randomString <- c(1:n)                  # initialize vector
    for (i in 1:n)
    {
        randomString[i] <- paste(sample(c(0:9, letters, LETTERS),
                                 lenght, replace=TRUE),
                                 collapse="")
    }
    return(randomString)
}

#  > MHmakeRandomString()
#  [1] "XM2xjggXX19r"

###############################################################

Now I can use the function to generate a filename when opening a new png device.



8 Responses to “R: Generate random string name”

  1. 1 changhong

    object name with a number as its first letter is not eligible in R. I think it’d better restrict the first letter to be alphabet only.

    Best,

  2. 2 markheckmann

    Hi Changhong,
    This surely is an approvement! Thanks.
    So the improved code would look like this now:

    makeRandomString <- function(n=1, lenght=12)
    {
    randomString <- c(1:n) # initialize vector
    for (i in 1:n)
    {
    randomString[i] <- paste( c(sample(c(letters, LETTERS), 1),
    sample(c(0:9, letters, LETTERS),
    lenght – 1, replace=TRUE)),
    collapse=””, sep=””)
    }
    return(randomString)
    }

    Mark

  3. 3 jordan

    Very nice, thanks.

  4. 4 João

    A more compact version with 52/102 chance of choosing a letter and 50/102 chance of choosing a number would be

    getRandString<-function(len=12) return(paste(sample(c(rep(0:9,each=5),LETTERS,letters),len,replace=TRUE),collapse=''))

    # Test it!
    getRandString()

  5. Useful, thanks!

  6. After giving it some thought, I made my function to fit my needs like so:

    random.string <- rep(NA, 8)
    randomizeString <- function(x) {
    a <-sample(letters, 1, replace = TRUE)
    return(a)
    }
    paste(c(sapply(random.string, randomizeString, simplify = TRUE), ".inp"), collapse = "")

    It shouldn't be hard to put it inside another function call to make it more readable (and portable).

  7. Sorry for littering your comments, but here is the above hinted function. :)

    randomizeFileName <- function(n = 8, ext) {
    random.string <- rep(NA, n)
    randomizeString <- function(x) {
    a <-sample(letters, 1, replace = TRUE)
    return(a)
    }
    file.name randomizeFileName(n = 8, ext =”tra”)
    [1] “ttmnzetx.tra”

  8. 8 dalazal

    One can also use the tempfile function from the base package.
    type ?tempfile to see how it works.



%d bloggers like this: