Announcement

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

  • Stata; tell me a joke!

    Hey guys.

    So this is basically more of a fun thing than anything really super useful, but I'm teaching myself how to write programs for STATA and I have an idea. I want to make stata to be able to tell me a random joke predefined from a list of 100 random jokes I've found when i type:

    Code:
    joke
    My problem is, that I don't know how to make it display ONE random line of string from a list of strings inside the ado-file itself. Any ideas?

    Sorry for the silly post

  • #2
    STATA, stata, Stata: only one of these is right.

    Rose Medeiros, now at StataCorp, wrote one such program in 2006 and publicised it on Statalist, but it was a rather narrow collection of jokes. I can't find the code on this machine, but I think the programming trick is just to define a series of macros and then call up one randomly:

    Code:
    program joke
    version 8.2
    
    local joke1 "In order to solve this differential equation, you stare at it until the solution occurs to you (G. P{c o'}lya)"
    local joke2 "What is mind? No matter! What is matter? Never mind! (19th century at least)"
    local joke3 "Variance is the attitude of one statistician to another (R.A. Fisher)"
    local joke4  "Using the data to guide the data analysis is almost as dangerous as not doing so (Frank Harrell)" 
    
    local random = ceil(4 * runiform())
    di as smcl "{p}`joke`random''{p_end}"
    
    end
    Last edited by Nick Cox; 03 Apr 2016, 03:53.

    Comment


    • #3
      Rolf Lund if you are comfortable using Mata at all, you could store your jokes in a string matrix/vector and then select an integer value between 1-100 to identify which element of the matrix/vector to display. Depending on the length of the joke, you could also use Nick Cox's suggestion with a modification to store the jokes persistently in a data set:

      Code:
      // Drop the program if already loaded in memory
      cap prog drop joke
      
      // Define a program
      program joke
      
          // Set the version under which Stata should interpret the syntax
          version 8.2
      
          // Preserve the current state of the data currently in memory
          preserve
      
              // Check for a data set with all of your jokes that persists independently 
              // from the program itself
              cap: confirm file `"`c(sysdir_personal)'jokes.dta"'
      
              // If the file doesn't already exist
              if _rc != 0 {
              
                  // Clear any data currently loaded in memory
                  clear
                  
                  // Set the number of observations
                  qui: set obs 4
                  
                  // Store jokes in the joke variable 
                  qui: g joke = "In order to solve this differential equation, you stare at it until the solution occurs to you (G. P{c o'}lya)" in 1
                  qui: replace joke = "What is mind? No matter! What is matter? Never mind! (19th century at least)" in 2
                  qui: replace joke = "Variance is the attitude of one statistician to another (R.A. Fisher)" in 3
                  qui: replace joke =  "Using the data to guide the data analysis is almost as dangerous as not doing so (Frank Harrell)" in 4
      
                  // Save the dataset along the path so it can be found easily regardless 
                  // of the end user's operating system
                  qui: save `"`c(sysdir_personal)'jokes.dta"', replace
                  
              } // End IF Block to create initial file
              
              // If the file does already exist load the file into active memory
              else  qui: use `"`c(sysdir_personal)'jokes.dta"', clear
                  
              // Print a random joke to the screen 
              di as smcl joke[ceil(`c(N)' * runiform())]
      
          // Return to the state of the data at the time the program was called
          restore
      
      // End of the program definition    
      end
      
      . joke
      In order to solve this differential equation, you stare at it until the solution occurs to you (G. Pólya)
      
      . joke
      In order to solve this differential equation, you stare at it until the solution occurs to you (G. Pólya)
      
      . joke
      Using the data to guide the data analysis is almost as dangerous as not doing so (Frank Harrell)
      
      . joke
      What is mind? No matter! What is matter? Never mind! (19th century at least)
      One thing you will probably start to identify quickly is that there are a lot of different ways to write an equivalent program. The difference is based on the use case/design. One thing that I found and continue to find useful/helpful is to revisit an idea you worked on previously a few months down the road and compare how you originally approached it with the approach you took at a later date. It definitely helps to identify patterns that you tend to rely on out of immediate convenience that you later avoid for more elegant solutions.

      Comment


      • #4
        Rolf, I wrote an ado in 2014 to do this. Code is below. Additional features include the ability of a user to add jokes to the program, a time delay for jokes which have a punchline, and an optional flag to have a given subset of jokes come up more regularly. All the jokes below are fun for the whole family, but statistical types will enjoy the "fhl" flagged morsels.



        capture program drop joke
        program define joke
        syntax , [new(string) sleep fhl slp(string) ]
        quietly {
        preserve
        clear
        capture confirm file "C:\Users\\`c(username)'\Dropbox\ado\personal\j\jo ke.ado"
        if _rc==0 {
        insheet using "C:\Users\\`c(username)'\Dropbox\ado\personal\j\jo ke.ado", clear nonames delimiter(~)
        local path "C:\Users\\`c(username)'\Dropbox\ado\personal\ j"
        }
        else {
        insheet using "C:\ado\personal\j\joke.ado", clear nonames delimiter(~)
        local path "C:\ado\personal\j"
        }
        if "`new'"!="" {
        tempvar dbase
        capture file close `dbase'
        file open `dbase' using `path'\joke.ado , write text append
        file write `dbase' `"// `new' "' _n
        file close `dbase'
        di in white "Joke added."
        }
        gen linenum=_n
        sum linenum if v1=="//BEGINNER" , meanonly
        drop in 1 / `r(mean)'
        drop if v1=="/*"
        drop if v1=="*/"
        drop if v1==""
        drop if v1=="end"
        drop if v1=="}"
        if "`fhl'"!="" {
        levelsof v1, local(vs)
        foreach v in `vs' {
        local foo: word 1 of `v'
        if "`foo'"!="//fhl/" {
        drop if v1=="`v'"
        }
        }
        }
        * record indicator
        drop linenum
        gen linenum=_n

        * need a random number for the joke.
        local N = _N
        capture drop goo
        *time
        *local ti = (`r(T24)')*100
        local ti : di %5.4f "$S_TIME"
        lstrfun ti , subinstr("`ti'",":","",.)
        local ti= substr("`ti'",4,.)
        di "`ti'"

        gen goo = uniform()
        replace goo = trunc(round(goo*1000+`ti',1))
        local foo = goo[4]
        set seed `foo'

        gen foo = uniform()
        replace foo = foo* `N'
        replace foo = trunc(round(foo,1))
        replace foo = 1 if foo==0
        local goo = foo[1]

        keep if linenum==`goo'
        levelsof v1 , local(joke) clean
        local joke = subinstr("`joke'","// ","",.)
        local joke = subinstr("`joke'","//fhl/ ","",.)
        replace v1 = "`joke'"

        * Tell the joke.
        noisily levelsof v1 , local(joke) clean
        if "`sleep'"!="" {
        sleep 3000
        }
        if "`slp'"!="" {
        sleep `slp'
        }
        * Punchline.
        noisily levelsof v2 , local(punchline) clean

        restore
        }
        end
        //BEGINNER
        //fhl/ How much does a polar bear weigh? ~ Enough to break the ice! Hi `c(username)', I'm Stata.
        //fhl/ Did you here about the biostatistician whose Normal distribution lost its whole left side? ~ It’s alright now.
        // What do you call a fly with no wings? ~ A walk!
        // Two snowmen are standing next to each other in a yard. ~ One says to the other, Funny, I smell carrots too.
        // What did the cat say after eating two robins lying in the sun? ~ I just love baskin’ robins.
        // What does a kangaroo and a zucchini have in common? ~ Neither one can ride a bike
        //fhl/ What did the neuropsychologist and the biostatistician have in common? ~ They were cold on the ferry ride over
        // Want to hear a pizza joke? nah, it’s too cheesy. What about a construction joke? ~ Oh never mind, I’m still working on that one.
        // Did you hear the joke about the rope? ~ Skip it.
        // Man: Is this seat empty? ~ Woman: Yes, and this one will be if you sit down.
        // What room has no solid walls? ~ A mushroom!
        //fhl/ Pessimist: Oh, this can’t get any worse! ~ Optimist: Yes, it can!
        //fhl/ I used to think the brain was the most important organ. ~ Then I thought, look what’s telling me that.
        // Did you hear the joke about the tortilla? ~ It was corny.
        // Wanna hear a joke about sound? ~ Nevermind, I think you already heard it.
        //fhl/ How do you throw a party on Pluto? ~ You planet.
        // Why can't a bicycle stand on it's own? ~ It's two tired.
        // A lion would never cheat on his wife, ~ but a tiger wood.
        // I came across a man with a shovel in a cemetery. Do you dig graves?, I asked. ~ Yeah, yeah they're all right.
        // What do you call it when you throw a hand grenade into a French kitchen? ~ Linoleum Blownapart
        // Did you hear the one about the wooden car with the wooden engine? ~ Well, it wooden go.
        // Why did the lion get lost? ~ Because the jungle is massive.
        // Have you heard the one about the three holes in the ground? ~ Well, well, well.
        // What's the oldest rock group in all of Europe? ~ Stonehenge.
        // Where do you find hippies? ~ At the top of your leggies!
        // I was walking along the street yesterday when some bloke threw milk and cheese at me. ~ I thought, how dairy!
        // What's a Pirate's favourite cheese? ~ Chedd-AAAAAARGH!!!
        // What cheese is made backwards? ~ Edam
        // A mushroom is sitting at a bar and notices that nobody is talking to him. ~ He finally gets up and says Why isn't anyone talking to me? I'm a fungi.
        // Did you hear about the fire at the circus? ~ It was intense.
        //fhl/ What'd the ocean say to the beach? ~ Nothing it just waved...
        // What did Mr. and Mrs. Hamburger name their daughter? ~ Patty
        //fhl/ What do you do when you have a longitudinal study, missing data, and want to run a parallel process growth curve model? ~Dot your i's and cross your t's
        //fhl/ What do you do when you can't remember if the data are coded 0=incorrect 1=correct, or the other way around? ~Mind your P's and Q's!
        // What is a Pirate's favorite letter?~R......AAAAAAARGH!!!
        // What is an Astronaut's favorite key on the keyboard? ~Space!
        // Two peanuts were walking down the street...~One of them was a salted. Assaulted. Get it?
        //fhl/ What did Zero say to number Eight? ~ Nice belt.
        //fhl/ Two chemists walk into a bar. The first one says I’ll have some H2O. The second one says, I’ll have some H2O too. and then he dies.
        // Helium walks into a bar. The bartender says we don't serve your kind here. ~ The helium doesn't react.
        // What does a nosy pepper do? ~ Get jalapeno buisness.
        // What do you call a boomerang that doesn't come back? ~ A stick
        // Where are otters from? ~ Otter Space
        // What do you call a bear with no teeth? ~ A Gummy Bear.
        // A sandwich walks into a bar, looks at the bartender and says, Gimme a whiskey, neat. Bartender says, ~ Sorry sir, we don't serve food here.
        // A grasshopper walks into a bar. The bartender says Hey, we've got a drink named after you. ~ Grasshopper says You've got a drink named Steve?
        // How do you get a dog to stop barking in the front seat? ~ Put him in the back seat.
        //fhl/ How do you convince Dan Mungas to get more beer? ~ Ask nicely.
        // What do you call a doe born without eyes? ~ No idear
        // Did you hear about the new corduroy pillows? ~ They're making headlines everywhere!
        // What's a pirate's favourite letter? ~ You think it's the R but it's really the C
        //fhl/ What's a statistician's favorite letter? ~ R.
        // What kind of shoes do ninjas wear? ~ Sneakers!!
        // Why can't your nose be 12 inches long? ~ Because then it'd be a foot!
        // A skeleton walks into a bar and says to the bartender, Gimme a beer and a mop.
        // Two fish are in a tank. ~ One turns to the other and says, how do you drive this thing?
        // How do you catch a squirrel? ~ Climb a tree and act like a nut
        // What's red and looks like a bucket? A red bucket. What's blue and looks like a bucket? ~ A red bucket in disguise.
        // Where did Napoleon keep his armies? ~ In his sleevies.
        // What did one lawyer say to the other lawyer? ~ We are both lawyers.
        // Why do ducks have flat feet? ~ To stomp out forest fires.
        //fhl/ Want to see 2 short jokes and a long one? ~ Joke joke jooooooooke
        // Why don't seagulls fly over bays? ~ Because then they would be bagels!
        // Why do hippos wear red toenail polish? To hide in cherry trees. ~ Have you ever seen a hippo in a cherry tree? No Must be working!
        // What did batman say to Robin before getting in the batmobile? ~ Get in the batmobile.
        // Two cannibals are eating a clown, one looks over to the other and says: ~ Does this taste funny to you?
        //fhl/ I asked a statistician for her number. ~ She gave me an estimate.
        //fhl/ A statistician's wife had twins. He was delighted. He rang the minister who was also delighted. Bring them to church on Sunday and we'll baptize them, said the minister. ~ No, replied the statis
        > tician. Baptize one. We'll keep the other as a control.
        //fhl/ You Might Be a Statistician if ~ you are 95% confident no one wants your job.
        //fhl/ You Might Be a Statistician if ~ you found accounting too exciting.
        //fhl/ You Might Be a Statistician if ~ you may not be normal but you are transformable.
        //fhl/ Did you hear about Workaholics Anonymous? ~ Me, neither. No time for the meetings.
        //fhl/ what did the baby statistics package call its father? ~ Data
        // How do you make a tissue dance?~Put a little boogie in in!
        //fhl/ Two epidemiologists are driving along admiring the scenery. Look at the rock on top of that mountain, says the first epidemiologist. ~ On their return journey the second epidemiologist says: Loo
        > k at the rock on top of that mountain—it’s still there!
        // What did Ernie say when Burt asked him if he would like ice cream? ~ SHERBERT!
        // What did Tarzan say when he heard the elephants coming over the hills? ~ Here come the elephants over the hills.
        // Why can't a T-Rex clap? ~ They're extinct.
        // Whats green and has wheels? ~ A pickle. I lied about the wheels.

        //fhl/ What do you get when you cross a joke with a rhetorical question? ~ A phd dissertation
        // How did the buffalo say good bye to his kid? ~ Bi-son!
        // Did you hear about the two guys who stole a calendar? ~ They each got six months.
        // Whats orange and sounds like a parrot? ~ a carrot.
        // What do you call a sleepwalking nun? ~ A roamin' Catholic.
        // What's green, has four legs and could kill you if it fell from a tree? ~ A pool table.
        // What's big, grey and can't climb a tree? ~ A parking lot.
        // Why do they put fences around cemeteries? ~ Because people are dying to get in.
        // Three vampires walk into a bar. The first vampire says, I'll have a blood. The second vampire adds, Yeah - a blood for me, too. The third vampire mulls it over, then says, I'll just have a plasma. ~
        > Alright, says the bartender. Two bloods and a blood light.
        // What jackets smell the worst? ~ Wind Breakers.
        // Did you hear the joke about the farmer? ~ Never mind it's too corny
        // What about the pizza joke? ~ Nah, forget it. It's too cheesy.
        //fhl/ Want to hear a joke about marathons? ~ Never mind, it takes too long to finish.
        // What is Mario's favorite fabric? ~ Denim Denim Denim
        // What's brown and sounds like a bell? ~ A brown bell
        // Why was Abraham Lincoln released from jail? ~ Because he was in a cent.
        // What do you call an alligator in a vest? ~ An investigator.
        //fhl/ Why shouldn't you write papers with a broken pencil? ~ Because it's pointless.
        // How do you open a locked banana? ~ With a MonKEY, of course.
        // What's ET short for? ~ Because he's got little legs.
        //fhl/ Why did the hipster psychometrician scald she mouth with his coffee? ~ Because she drank it before it was cool.
        // What's green and has wheels? ~ Grass. I lied about the wheels.
        // What kind of shoes does a chicken wear? ~ Ree bok bok boks
        // What did the really polite monkey say to the scientist? ~ May I please have another banana?
        // Why are there no knock knock jokes about America? ~ Because FREEDOM RINGS!
        //fhl/ An infinite number of mathematicians walk into a bar. The first one orders a drink. The second one orders half a drink. The third one orders a quarter of a drink. ~ The bartender stops them and
        > says, Know your limits boys, and pours them two beers.
        // How do you make a hot dog stand? ~ Take away its chair!
        //fhl/ How do you free up time for an epidemiologist? ~ Take away its chair.
        //fhl/ Why isn't the epidemiologist's nose 12 inches long? ~ Because then it would be a foot!
        //fhl/ What's the difference between a dog and an epidemiologist? ~ They both bark. Except the Epidemiologist.
        //fhl/ Did you hear about the camping trip at Friday Harbor? ~ It's going to be in tents!
        // Why did it get so hot after the superbowl ended? ~ All of the fans left.
        // What do you call an egg that's scared? ~ A chicken egg.
        //fhl/ Why can't psychometricians play cards in the jungle? ~ too many cheetahs.
        // How can you tell if there's an elephant in your bed? ~ He has an E on his pajamas.
        // What did the mama volcano say to the baby volcano? ~ I lava you.
        // Wanna hear a cat joke? ~ Just kitten.
        //fhl/ Two neurologists walk into a bar. ~ The epidemiologist following them observed this, then ducked.
        //fhl/ Why does everyone like the epidemiologist? ~ Because his laugh is infectious
        // Why don't you ever want to run in front of a car? ~ You'll get tired.
        // Why don't you ever want to run behind a car? ~ You'll get exhausted.
        // Why couldn't the bicycle stand up? ~ Because it was two tired.
        // When is a door not a door? ~ When its ajar.
        // Have you ever tried to eat a clock? ~ It is very time consuming.
        // What do you call a million rabbits walking backwards? ~ A receding hareline!
        // What's grey and doesn't matter? ~ An irrelephant!


        Comment


        • #5
          Hi all,

          There is an updated version of this idea in github. It uses APIs to retrieve jokes. I like it.

          https://github.com/randrescastaneda/joke

          Best,

          Pablo
          Best,
          Pablo Bonilla

          Comment


          • #6
            Nicely written command! I like it too.

            A rough one just to demonstrate Stata's -copy- command's web capability:

            Code:
            *! version 1.0.0  05mar2019
            program define geekjokes
                tempfile f
                copy "https://geek-jokes.sameerkumar.website/api" `"`f'"'
                type `"`f'"'
            end
            Last edited by Hua Peng (StataCorp); 05 Mar 2019, 20:32.

            Comment

            Working...
            X