First we need to import some modules to the program.

import json, random, textwrap, requests

Next we will need a way to get a random number between 1 and 20, we will call this value randompost.This will be used later to get a random post.We skip post 0 since that is just an announcement for the subreddit

randompost = random.randint(1,20)

Now we will grab the json data. We will be getting the hot posts, which is the best recent posts. According to the Reddit api we need to identify ourselves by putting in a User Agent. Im calling this bot Showerthoughtbot 0.1, but if this stops working we will need to rename it.

url = requests.get('https://www.reddit.com/r/showerthoughts/hot.json', headers = {'User-agent': 'Showerthoughtbot 0.1'})

Then we call that data reddit

reddit = json.loads(url.text)

Then we display the post. We want the title of the post. We use textwrap so that after 32 characters it starts a new line.I picked 32 so the text doesnt scroll too far in the screen. Depending on how you display this you may need to adjust it.

print textwrap.fill((reddit['data']['children'][randompost]['data']['title']),32)