Coding Challenge #42.1: Markov Chains – Part 1

In Part 1 of this Coding Challenge, I discuss the concepts of “N-grams” and “Markov Chains” as they relate to text. I use Markova chains to generate text automatically based on a source text.

Programming from A to Z – Markov Chains URL:

Part 2:

Support this channel on Patreon:

Send me your questions and coding challenges!:


GitHub Repo with all the info for Programming from A to Z:

Links discussed in this session:
Google’s Ngram Viewer:
n-gram on Wikipedia:
Chris Harrison’s Web Trigrams:
Allison Parrish’s( Generative Course Descriptions:
Allison Parrish’s website:
Michael Walker’s King James Programming:
Victor Powell’s Markov Chains Explained:
Flooper’s Perlin Noise song:

Source Code for the all Video Lessons:


For More Programming from A to Z videos:

For More Coding Challenges:

Help us caption & translate this video!






39 responses to “Coding Challenge #42.1: Markov Chains – Part 1”

  1. roff poff Avatar

    is that Java or c++ ?

  2. Don't Read My Profile Picture Avatar

    I've been writing a markov chain in python, and what I'm doing is creating a list of all words I've seen, as well as all 2 word pairs, 3 word pairs etc for each order we want to go up in. I also store what words come after with an array whose indexes correspond to which word they follow in the list of seen. I then use an algorithm that chooses the highest order markov chain based on the current output and adds that word (word can be a single character). that keeps going for however long I want the output to be.

  3. Sanidhya Sinha Avatar

    first of all thanks for the video, could you help me with computing the n grams and then creating a hash value for thegrams individually ? ALL THIS IN PYTHON

  4. crook Avatar

    Daniel, you're doing a great job. I want to learn more about machine learning.
    and specifically unsupervised learning. I want to do something with that. thanks once again.. if you can then make more videos on Machine learning

  5. Yuqing Wang Avatar

    you are sososo good

  6. SpiderArtMC Avatar

    "The Unicorn is a virgin" I guess that is why they died out. (Text generated in video)

  7. SpiderArtMC Avatar

    I actually built a similar thing to this once. I took a bunch of text, usually 20-30 words, and found what came after each letter: e might be 3/8 t, 2/8 r, 1/8 c, 1/8 h, and 1/8 l. I would find random generations of words forming sentences that sound like the language the words came from. (With Spanish training data, the result sounded Spanish)

  8. Don Quixote Avatar

    Please do this video for Python.

  9. Daniel Poda Avatar

    great intro to Markov Chains. Got inspired to build a tweet generator

  10. Slacker Avatar

    This channel is amazing, thank you so much!

  11. Dusty923 Avatar

    Someone please take Trump's tweets and create a Twitter bot that tweets randomly generated Trump tweets.

  12. Raja Williams Avatar

    antidisestablishmenterianism coding

  13. kevnar Avatar

    Wow. I built this whole project myself one day, analyzing a novel's worth of text for the statistics of what words come after other words, and then producing random text with those same probabilities. I didn't even know there was a name for it. I was just bored one day.

    The text was mostly gibberish in the end, but it was pretty cool gibberish.

  14. JoshyRobot Avatar

    with the unicorns sample text I got a mixture of Renaissance, ancient, and antiquity. Renaissancientiquity!

  15. exnerhimself Avatar

    you should use youtube's automatically generated subtitles of your videos as source text

  16. julz2020 Avatar

    fantastic pronunciation on that final paragraph! :]

  17. Mak X Avatar

    can this solution be stated under the heading of machine learning?

  18. realcygnus Avatar

    Damn, you read and speak Markovese so fluently………Rosetta Stone or were you born there ?

  19. metacarpo10 Avatar

    Daniel, first of all, thank you very much for your videos.
    I'm trying to find with no success if there is a way to use the idea of the markov chain to, instead of reading and generating text, to read and generate music! Im trying to use a RAW audio format to convert it to a byte array but I'm not sure I'm doing it right and It does not work. I'm trying to do that in processing just for better performance instead of using p5.js, do you know if that is even possible? Genarating some sort of music direct from bytes from another audio?. There's a new codding challenge for you!

  20. Sergio Apreda Avatar

    Hi Daniel, I started watching your content only recently, and find it very enjoyable and informative. Just a quick question: in line 10 shouldn't that 3 changed with "order" ? i think you hardcoded the 3 somwhere else in the code but don't have time to re-watch the whole thing now… Anyways keep up the good work, you are most likely the most cheerful programmer i've ever seen :^)

  21. Learn IT Avatar

    Man I can not find the words to describe how happy I am for finding your channel, you have made my life about 2x better with making such a nice videos, keep on rocking.

  22. Starogre Avatar

    i could watch your videos forever and never program anything myself. got any advice on how to separate the time spent?

  23. Kobe Miller Avatar

    He got the Compile Error because var Result was called already. And the scope of result was also included the for loop, so have var result in the for loop was redundant because the result was already defined. Am I right? That's my assumption based on my knowledge of being fluent with Java. I'm starting to learn HTML and Javascript, so if anything I said was wrong, can anyone explain to me why? That is my basic understanding, though. I could be wrong, though.

  24. Ilustrado Avatar

    Daniel! It would be interesting to hear your thoughts about the Library of Babel.

  25. Thibaut Vercueil Avatar

    Awesome video! Really interesting.

  26. trien pham Avatar

    what font and syntax highlighting do you use?

  27. fred forcx Avatar

    Great video!
    Can we use that to generate music?

  28. jeff ka Avatar

    your YouTube subscribers are growing, remember me, I was here from the start.

  29. Chris Lamothe Avatar

    Dan please help! I'm new to programming and was looking for a place to start. I've played around with Processing and p5.js, and I'm extremely interested in the Nature of Code, and now I see your Programming from A to Z videos. I seem to be confusing myself with all the options. I have no coding experience, so I know I can't start with Nature of Code, but I'd like to get to that point! Where should I start?

  30. Eitan Asher Avatar

    what ide is he using?
    my p5 doesnt look like that, and if it's atom, is there any video where he explains how to setup?
    thanks for your help!

  31. Jared Nix Avatar

    Such a ball of energy, your videos have helped me so much in learning to code.
    Never a dull moment and always able to keep my attention 🙂 Thanks and keep it up!

  32. Asa Taylor Avatar

    "The erroneously beast with the unicorn is of natured." Great stuff on fascinating subject matter. I'm gonna try this with whole words instead of n-grams.

  33. Dominic Forster Avatar

    It got me confused, so I had to watch it twice 😛

  34. Alexander Kraus Avatar

    15:14 Can someone please make a song of this "r or r or i"

  35. cantabr0 Avatar

    "this followed by dot" 😀 Also, you are an excellent teacher!

  36. Daniel Vasu Avatar

    i love your videos !

Leave a Reply

Your email address will not be published. Required fields are marked *