Announcement

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

  • Generate random integers

    Hello Listers

    i want to make a mock dataset

    i would like to generate 2 integers at random
    1) either 0 or 1
    2) between 18-99
    3) either 0 or 1 but at a ratio of 5 to 1

    thank you.

    best
    lars



  • #2
    Your post confuses me. You say you want to generate 2 integers at random, and then you list 3 descriptions. So then I thought perhaps you meant that you wanted three random variables with N = 2, but you can't have a 5 to 1 ratio of anything in an N of 2. So I'm going to guess that the 2 was a typo.

    Code:
    gen wanted1 = runiformint(0, 1)
    gen wanted2 = runiformint(18, 99)
    gen wanted3 = runiformint(0, 5)
    replace wanted3 = 1 if wanted3 > 1
    Note: The code I gave for wanted 3 will geneate 5 1's for each 0. If you meant the other way around (I couldn't tell) then just follow that with -replace wanted3 = !wanted3-.

    And don't forget to set the random number generator seed before you start so that you will be able to reproduce your results.
    Last edited by Clyde Schechter; 27 Dec 2020, 10:13.

    Comment


    • #3
      A summary can be found here: https://blog.stata.com/2012/07/18/us...rators-part-1/


      Code:
      clear all
      set obs 9999
      set seed 123
      
      gen r = runiform()
      
      *1.)
      gen a = cond(r < 0.5, 0, 1)
      
      *2.)
      gen b = floor((99 - 18 + 1) * r + 18)
      
      *3.)
      gen c = cond(r < 0.20, 0, 1)
      
      summarize *
      Best wishes

      (Stata 16.1 MP)

      Comment

      Working...
      X