Announcement

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

  • Generate random integer number from a list but excluding one number from that list

    Dear all,

    I want to process a simple idea, but I have real difficulties to operate it into Stata.

    The idea: Imagine a set of 500 schools for which I randomly assign 12 enumerators that have to visit those schools. But for each school I also want to randomly assign an enumerator (from the same list) that will check the work of the first selected enumerator. In other words, for each school I want to randomly assign one enumerator and a second one from the same pool of enumerators but obvioulsy excluding the first one. For example, if enumerator 5 is firstly randomly selected I want to selected an enumerator code between [1-12] excluding 5.

    Is there a simple way to code that?

    Thanks a lot for any help you could provide,
    Best

    Code example:
    clear all
    set obs 500
    gen School = _n
    gen EnumeratorCode = runiformint(1,12)
    gen EnumeratorCode_Check = . //This is where I'm stuck






  • #2
    A crude way is just to loop until values are all different: Here groups is from the Stata Journal and just one way to tabulate the results (a 12 x 12 table might look a little messy).

    Code:
    clear all
    set obs 500
    set seed 2803  
    gen School = _n
    gen Code = runiformint(1,12)
    gen Check = . 
    gen Trythis = . 
    count if Check == . 
    
    while r(N) > 0 { 
        quietly replace Trythis = runiformint(1, 12) 
        quietly replace Check = Trythis if Check == . & Trythis != Code 
        count if Check == . 
    } 
    
    groups Code Check
    Here are my results:

    Code:
     
     43
      4
      0
    
    . 
    . groups Code Check 
    
      +--------------------------------+
      | Code   Check   Freq.   Percent |
      |--------------------------------|
      |    1       2       4      0.80 |
      |    1       3       3      0.60 |
      |    1       4       4      0.80 |
      |    1       5       1      0.20 |
      |    1       6       7      1.40 |
      |--------------------------------|
      |    1       7       3      0.60 |
      |    1       8       3      0.60 |
      |    1       9       4      0.80 |
      |    1      10       3      0.60 |
      |    1      11       1      0.20 |
      |--------------------------------|
      |    1      12       3      0.60 |
      |    2       1       4      0.80 |
      |    2       3       5      1.00 |
      |    2       4       5      1.00 |
      |    2       5       5      1.00 |
      |--------------------------------|
      |    2       6       4      0.80 |
      |    2       7       3      0.60 |
      |    2       8       4      0.80 |
      |    2       9       4      0.80 |
      |    2      10       7      1.40 |
      |--------------------------------|
      |    2      11       4      0.80 |
      |    2      12       2      0.40 |
      |    3       1       3      0.60 |
      |    3       2       4      0.80 |
      |    3       4       4      0.80 |
      |--------------------------------|
      |    3       5       4      0.80 |
      |    3       6       5      1.00 |
      |    3       7       1      0.20 |
      |    3       8       8      1.60 |
      |    3       9       3      0.60 |
      |--------------------------------|
      |    3      11       4      0.80 |
      |    3      12       2      0.40 |
      |    4       1       3      0.60 |
      |    4       2       2      0.40 |
      |    4       3       5      1.00 |
      |--------------------------------|
      |    4       5       2      0.40 |
      |    4       6       5      1.00 |
      |    4       7       5      1.00 |
      |    4       8       4      0.80 |
      |    4       9       4      0.80 |
      |--------------------------------|
      |    4      10       4      0.80 |
      |    4      11       3      0.60 |
      |    4      12       9      1.80 |
      |    5       1       3      0.60 |
      |    5       2       4      0.80 |
      |--------------------------------|
      |    5       3       4      0.80 |
      |    5       6       4      0.80 |
      |    5       7       4      0.80 |
      |    5       8       2      0.40 |
      |    5      10       5      1.00 |
      |--------------------------------|
      |    5      11       3      0.60 |
      |    5      12       3      0.60 |
      |    6       1       3      0.60 |
      |    6       2       2      0.40 |
      |    6       3       6      1.20 |
      |--------------------------------|
      |    6       4       5      1.00 |
      |    6       5       2      0.40 |
      |    6       7       4      0.80 |
      |    6       8       3      0.60 |
      |    6       9       5      1.00 |
      |--------------------------------|
      |    6      10       2      0.40 |
      |    6      11       3      0.60 |
      |    6      12       4      0.80 |
      |    7       1       2      0.40 |
      |    7       2       7      1.40 |
      |--------------------------------|
      |    7       3       2      0.40 |
      |    7       4       2      0.40 |
      |    7       5       5      1.00 |
      |    7       6       6      1.20 |
      |    7       8       2      0.40 |
      |--------------------------------|
      |    7      10       4      0.80 |
      |    7      11       2      0.40 |
      |    7      12       2      0.40 |
      |    8       1       1      0.20 |
      |    8       2       5      1.00 |
      |--------------------------------|
      |    8       3       4      0.80 |
      |    8       4       4      0.80 |
      |    8       5       1      0.20 |
      |    8       6       7      1.40 |
      |    8       7       4      0.80 |
      |--------------------------------|
      |    8       9       5      1.00 |
      |    8      10       3      0.60 |
      |    8      11       2      0.40 |
      |    8      12       3      0.60 |
      |    9       1       1      0.20 |
      |--------------------------------|
      |    9       2       3      0.60 |
      |    9       3       5      1.00 |
      |    9       4       4      0.80 |
      |    9       5       6      1.20 |
      |    9       6       5      1.00 |
      |--------------------------------|
      |    9       7       6      1.20 |
      |    9       8       7      1.40 |
      |    9      10       3      0.60 |
      |    9      11       6      1.20 |
      |    9      12       2      0.40 |
      |--------------------------------|
      |   10       1       3      0.60 |
      |   10       2       3      0.60 |
      |   10       3       4      0.80 |
      |   10       4       5      1.00 |
      |   10       5       4      0.80 |
      |--------------------------------|
      |   10       6       3      0.60 |
      |   10       7       2      0.40 |
      |   10       8       3      0.60 |
      |   10       9       3      0.60 |
      |   10      11       5      1.00 |
      |--------------------------------|
      |   10      12       7      1.40 |
      |   11       1       8      1.60 |
      |   11       2       4      0.80 |
      |   11       3       2      0.40 |
      |   11       4       4      0.80 |
      |--------------------------------|
      |   11       5       1      0.20 |
      |   11       6       5      1.00 |
      |   11       7       6      1.20 |
      |   11       8       6      1.20 |
      |   11       9       2      0.40 |
      |--------------------------------|
      |   11      10       4      0.80 |
      |   11      12       3      0.60 |
      |   12       1       7      1.40 |
      |   12       2       4      0.80 |
      |   12       3       4      0.80 |
      |--------------------------------|
      |   12       4       6      1.20 |
      |   12       5       3      0.60 |
      |   12       6       4      0.80 |
      |   12       7       3      0.60 |
      |   12       8       8      1.60 |
      |--------------------------------|
      |   12       9       7      1.40 |
      |   12      10       4      0.80 |
      |   12      11       4      0.80 |
      +--------------------------------+

    Comment


    • #3
      Here is example code demonstrating an approach to your problem, and a tabulation that shows that the selections have the expected properties.
      Code:
      . set seed 666
      
      . set obs 1000000
      number of observations (_N) was 0, now 1,000,000
      
      . generate int1 = runiformint(1,12)
      
      . generate int2 = runiformint(1,11)
      
      . replace  int2 = int2 + 1 if int2>=int1
      (500,222 real changes made)
      
      . assert int2!=int1
      
      . tab int1 int2
      
                 |                               int2
            int1 |         1          2          3          4          5          6 |     Total
      -----------+------------------------------------------------------------------+----------
               1 |         0      7,590      7,755      7,420      7,554      7,488 |    83,256 
               2 |     7,593          0      7,506      7,599      7,524      7,574 |    83,348 
               3 |     7,450      7,671          0      7,513      7,545      7,487 |    83,216 
               4 |     7,565      7,575      7,585          0      7,606      7,532 |    83,178 
               5 |     7,581      7,470      7,534      7,486          0      7,589 |    83,204 
               6 |     7,789      7,529      7,480      7,559      7,375          0 |    83,474 
               7 |     7,689      7,587      7,476      7,804      7,637      7,627 |    83,809 
               8 |     7,612      7,545      7,543      7,674      7,628      7,643 |    83,346 
               9 |     7,538      7,652      7,704      7,500      7,637      7,533 |    83,390 
              10 |     7,624      7,742      7,422      7,535      7,569      7,643 |    83,323 
              11 |     7,673      7,524      7,656      7,495      7,523      7,626 |    83,304 
              12 |     7,524      7,539      7,528      7,667      7,569      7,543 |    83,152 
      -----------+------------------------------------------------------------------+----------
           Total |    83,638     83,424     83,189     83,252     83,167     83,285 | 1,000,000 
      
      
                 |                               int2
            int1 |         7          8          9         10         11         12 |     Total
      -----------+------------------------------------------------------------------+----------
               1 |     7,632      7,581      7,587      7,564      7,439      7,646 |    83,256 
               2 |     7,663      7,561      7,620      7,514      7,564      7,630 |    83,348 
               3 |     7,629      7,498      7,623      7,604      7,599      7,597 |    83,216 
               4 |     7,531      7,512      7,598      7,537      7,659      7,478 |    83,178 
               5 |     7,742      7,498      7,524      7,648      7,548      7,584 |    83,204 
               6 |     7,534      7,622      7,619      7,739      7,734      7,494 |    83,474 
               7 |         0      7,760      7,543      7,582      7,517      7,587 |    83,809 
               8 |     7,347          0      7,526      7,656      7,568      7,604 |    83,346 
               9 |     7,496      7,695          0      7,613      7,553      7,469 |    83,390 
              10 |     7,453      7,629      7,532          0      7,585      7,589 |    83,323 
              11 |     7,640      7,506      7,431      7,594          0      7,636 |    83,304 
              12 |     7,582      7,718      7,413      7,538      7,531          0 |    83,152 
      -----------+------------------------------------------------------------------+----------
           Total |    83,249     83,580     83,016     83,589     83,297     83,314 | 1,000,000

      Comment


      • #4
        Here's another approach. Begin by creating a census of all possible pairs of enumerators (not counting a pair of an enumerator with him/herself, and not counting pair x, y as different from y, x). Then randomly sample with replacement from that set of pairs:

        Code:
        clear*
        
        //  CREATE A DATA SET OF ALL ENUMERATOR PAIRS
        frame rename default enumerator_pairs
        set obs 12
        gen byte enumerator1 = _n
        expand _n-1
        by enumerator1, sort: gen byte enumerator2 = _n
        drop if enumerator1 == enumerator2
        gen int link = _n
        local n_pairs = link[_N]
        
        //  CREATE A DATA SET OF ALL SCHOOLS
        frame create schools
        frame change schools
        clear
        set obs 500
        gen int school = _n
        
        //  LINK THEM RANDOMLY
        set seed 1234 // OR WHATEVER SEED YOU LIKE
        gen int link = runiformint(1, `n_pairs')
        frlink m:1 link, frame(enumerator_pairs)
        frget enumerator1 enumerator2, from(enumerator_pairs)
        drop link enumerator_pairs // optional
        I assume you are using version 16, so you can use frames. If not, you are supposed to say so in your post (see Forum FAQ). In that case, you can accomplish the same thing by saving the enumerator pairs data in a -tempfile- and then accomplishing the linking by using -merge m:1-.

        My code produces a slightly different kind of results from William's in that mine does not produce both pairs (x, y) and (y, x): I assume that the two enumerators are interchangeable, it doesn't matter which we call 1 and which we call 2, since 2's job is to replicate 1's results, and they are drawn from the same pool of 12 people.

        Comment


        • #5
          For now, I used the following code that seems to work:

          clear all
          set obs 10000
          gen scode = _n

          set seed 872612
          gen rand1 = runiformint(1,7)
          gen rand2 = runiformint(1,6)

          replace rand2 = rand2 + 1 if rand1 == 1 & rand2 >= 1
          replace rand2 = rand2 + 1 if rand1 == 2 & rand2 >= 2
          replace rand2 = rand2 + 1 if rand1 == 3 & rand2 >= 3
          replace rand2 = rand2 + 1 if rand1 == 4 & rand2 >= 4
          replace rand2 = rand2 + 1 if rand1 == 5 & rand2 >= 5
          replace rand2 = rand2 + 1 if rand1 == 6 & rand2 >= 6

          tab rand1
          tab rand2


          However, if someone has a shorter command that could be really interesting

          Comment


          • #6
            Thanks a lot for all your reply (I didn't see them before posting my last post). So I can see that there is no "fast way" to do so.
            I see that those 3 methods are working well, which is really great for future reference.

            Thanks a lot for your help!

            Comment


            • #7
              Quick note: yur series of 6 -replace- statements can be reduced to a single command:

              Code:
              replace rand2 = rand2 + 1 if rand2 >= rand1
              which does exactly the same thing.

              Comment

              Working...
              X