A markov chain based random word generator
Clone or download
mrsharpoblunto Merge pull request #9 from sbj42/fix-maxlength
Fix issue #8 - infinite loop when maxLength is -1

Thanks for posting all these PR's! - the API for this library is a bit whacky, generateWord should really should accept an options object with optional properties, but this is a nice fix in absense of that.
Latest commit 73e640c Oct 13, 2018



Build Status npm version

A Javascript library which allows you to easily create Markov chains based on arbitrary dictionaries in order to create readable pseudo random words (Yes the name was generated by the library). See a demo of this library in action here

This library can be used from node.js, or from the browser using the browserify'd version in dist/foswig.js


var Foswig = require('foswig');

// Create the markov chain and specify the Order of the markov chain.
// The order (an integer > 0) indicates how many previous letters are 
// taken into account when selecting the next. A smaller order will
// result in more randomized less recognizeable output. Conversely a
// higher order will result in words which resemble more closely those
// in the original dictionary.
var chain = new Foswig(3);

// add words into the markov chain one at a time

//OR add all the words in an array at once
var dictionary = ["hello","foswig"];
// generate a random word with a minimum of 5 characters, a maximum of 10 letters, 
// and that cannot be a match to any of the input dictionaries words. 
// NOTE: if it is not possible to generate the desired word length from the input 
// dictionary, then this method will throw an Error after 25 failed attempts. This 
// can be customized by adding a final parameter to the method indicating the maximum 
// number of attempts to make when generating a word. 
//     e.g. chain.generateWord(5,10,false,100); for 100 attempts
var randomWord = chain.generateWord(5,10,false);


Foswig.js is licensed under the MIT license.