Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generate random strings containing letters and numbers

    Dear Statalist,

    I have 2 questions below:

    (1)
    I am wondering if there is a way to generate random strings with pre-defined digits and combinations?
    Specifically, I want a a total of 600 6-digit strings, in the format of [UPPER LETTER][NUMBER][UPPER LETTER][UPPER LETTER][NUMBER][UPPER LETTER], e.g. A3GF6Z.
    And I don't want duplicates within these 600 obs. I am not aware of how to do this with the combination of generating both letters and numbers

    In all, the conditions are

    #1: obs = 600
    #2: 6-digit strings
    #3: format of [upp letter][#][upp letter][upp letter][#][upp letter]
    #4: no duplicate
    #5: random


    (2)

    Also, when I tried syntax below to try to generate a 6-digit all upper letter random strings , it did not work on my StataMP 15, with error saying that "command ralpha is unrecognized r(199);"\

    Code:
    clear
    set obs 600
    gen random_string = ""
    local lrandom_string 6                 
    forval n = 1/`lrandom_string' {
     ralpha new, upp
     replace random_string = random_string + new
     drop new
    }

    Any help of the 2 questions is appreciated! Thanks!

    Yingyi

  • #2
    You can generate a random string with the correct combinations directly as follows. There's a slight chance that you could generate the same string more than once so you must generate extra strings, drop duplicates, and pick a random sample of 600:

    Code:
    clear
    set seed 2312
    set obs 1000
    gen random_string = char(runiformint(65,90)) + ///
        string(runiformint(0,9)) + ///
        char(runiformint(65,90)) + ///
        char(runiformint(65,90)) + ///
        string(runiformint(0,9)) + ///
        char(runiformint(65,90))
    
    bysort random_string: keep if _n == 1
    gen mixitup = runiform()
    sort mixitup
    keep in 1/600
    drop mixitup

    Comment


    • #3
      Originally posted by Robert Picard View Post
      You can generate a random string with the correct combinations directly as follows. There's a slight chance that you could generate the same string more than once so you must generate extra strings, drop duplicates, and pick a random sample of 600:

      Code:
      clear
      set seed 2312
      set obs 1000
      gen random_string = char(runiformint(65,90)) + ///
      string(runiformint(0,9)) + ///
      char(runiformint(65,90)) + ///
      char(runiformint(65,90)) + ///
      string(runiformint(0,9)) + ///
      char(runiformint(65,90))
      
      bysort random_string: keep if _n == 1
      gen mixitup = runiform()
      sort mixitup
      keep in 1/600
      drop mixitup
      Thanks Robert! This works perfectly!

      Comment


      • #4
        Hi all,

        With the runiformint command, is there a way to exclude a certain letter? For instance, I would not like the letter 'O' to be included in my character list. Therefore, I would only like the byte values (65,78 & 80-90), since the letter 'O' corresponds to 79. Is there a way to do this in syntax?

        Thanks in advance,
        Nicole
        Last edited by Nicole Carbert; 19 Feb 2018, 13:03.

        Comment


        • #5
          Here's how to tweak the code to draw a letter from a list of characters to use (stored in the c2use local macro). I've only changed the first character, you can extend this other characters if desired.

          Code:
          clear
          set seed 2312
          set obs 1000
          local c2use ABCDEFGHIJKLMNPQRSTUVWXYZ
          
          gen random_string = substr("`c2use'", runiformint(1,length("`c2use'")),1) + ///
              string(runiformint(0,9)) + ///
              char(runiformint(65,90)) + ///
              char(runiformint(65,90)) + ///
              string(runiformint(0,9)) + ///
              char(runiformint(65,90))
          
          bysort random_string: keep if _n == 1
          gen mixitup = runiform()
          sort mixitup
          keep in 1/600
          drop mixitup
          
          gen s = substr(random_string,1,1)
          tab s

          Comment


          • #6
            Thanks for your help Robert--that is exactly what I need!

            Comment

            Working...
            X