Looking for some dummy data -Take a chance with Chance

Robert Galvin -
3 MIN READ
2
2

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:

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

chance.name();

  => 'Tony Saunders'

How about and address?

chance.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:

chance.android_id()

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

chance.apple_token()

=> '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'

chance.mixin({

    'user': function() {

        return {

            first: chance.first(),

            last: chance.last(),

            email: chance.email()

        };

    }

});

// Then you can call your mixin

chance.user();

=> {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:

chance.d20();

=> 13

profile

Robert Galvin

Please Register or Login to post a reply

2 Replies

R Robert Galvin

Awesome Jon - thanks for sharing. I like that Faker also gives you image avatars.

J Jon Tara

Prefer to do this in Ruby?

The faker gem:

&nbsp;&nbsp;&nbsp; <a href="https://github.com/stympy/faker">https://github.com/stympy/faker</a&gt;

is pure Ruby, and so the lib is probably easily imported as a Rhodes extension.

As well, there are a number of similar Ruby Gems/libraries. But Faker is popular and currently-maintained.

I'm a big fan of libraries like this.