When you are dealing with developing applications that have a large data set, being able to test your application with as much random-as-real-as-you-can-get data is priceless and saves a boat load of time. There are many choices out there, but for some reason I never came across Chance.js. This library packs a punch for various types of data and is drop dead easy to use.


Download the library and reference it in your application and you instantly have access to tons of random data:


<script src="chance.js"></script>



The library is so intuitive to use you hardly have to consult the documentation. Need a name? No Problem:


  => 'Tony Saunders'


How about and address?


=> '5447 Bazpe Lane'


Maybe a French Phone Number:


chance.phone({ country: 'fr' });
=> '01 60 44 92 67'


Each method usually has some parameters that you simply pass in as an object and it will fine tune the data to exactly what you are looking for:


Say you want a random latitude and longitude coordinates, but you need to bound it to a certain area. No problem:


lastgeo:  {
           "__type": "GeoPoint",
           "latitude": chance.latitude({min: 35.7332563, max: 35.762792,fixed: 7}),
           "longitude": chance.longitude({min: -78.78245, max: -78.7597049,fixed: 7})


It even has some mobile specific randomness like, Android GCM Id or Apple Push Token:


=> 'APA91HHQyPlWqV2Nu61LRs41eE4vrR4bHLKTs0-Dh5nDLopcWZotLw77NEoJyADNJiq6cwY0jMM02y8aacLs6fe2_ynweFjZJVVevKON-32826v-EFoayyThU3-42YEUY9pCScU_n73yRNSOlTk5W6iPtrDkQ3a6_BvOxRbSYi3E6QEY0ZuIQF0'

=> 'b50edac575bfba07dd019b28b2af7189a3ddda17c806ef14a9abbfd00533f67e'



There are so many categories included like numbers, strings, people, web, location, time, etc, but it does not stop there. They throw in some helper functions as well. Say you had a list of specific things you wanted to pick from, but you wanted certain items to appear more often than others. Well, check out the weighted function:


Provide an array of items, and another array of items specifying the relative weights and Chance will select one of those items, obeying the specified weight.


chance.weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4]);
=> 'c'


Will generate a letter from the array but will pick 'b' twice as often as it picks 'a' and will pick 'c' three times as often as it picks 'a' and will pick 'd' four times as often as it will pick 'a' and will pick 'd' two times as often as it will pick 'b'.


You can even take it one step further, if you aren't already drooling. Why not extend chance with your own objects so that you can simply call chance with one line of code and get back an object with randomized data. They call this a 'mixin'


    'user': function() {
        return {
            first: chance.first(),
            last: chance.last(),
            email: chance.email()

// Then you can call your mixin

=> {first: 'Eli', last: 'Benson', email: 'gembibuj@dugesan.com'}



So go ahead and roll the dice and fill your app with a bunch of random data... O yes, they do have a function for 'dice'..It could even be a twenty sided dice for all you dungeons and dragons fans out there:


=> 13