Creating random strings for a dummy table

Today I worked on a JavaScript placed in a DirectSmile Crossmedia campaign, which is currently used for supporting auto-completion based on a list of entries coming from a DataRelation. The customer who uses this script asked if we could allow prefixes which will not being recognized by the auto-completion.

Well, sounded like a challenge – perfect for me as I like such things, challenge accepted! Before I could begin I had to get some dummy data… but how?

So I could just create an empty campaign, fill the database with dummy data I have to write on my own and then link it with a DataRelation to the real campaign.

This approach sounds like a plan – but on the other hand it sounds like a lot of manual work to do. And as software engineers are lazy by nature I had the idea for a slightly different approach:

With the latest release of DirectSmile CrossMedia a new feature has been introduced: Scripted DataRelations! This special DataRelation uses JavaScript to produce data. So all I had to do was create some JavaScript which will create my dummy string which fulfills my needs.

To create a random string in JavaScript all you need is a string with all wanted chars – in my case I separated the capital letters from the normal ones and placed them in two strings:


var possible = „abcdefghijklmnopqrstuvwxyz“;

Now you only need a random number between 0 and the length of each source-string:

for (var i = 0; i < 10; i++) {

randomString += possible.charAt(Math.floor(Math.random() * possible.length));


This will create you a random string with 10 characters. Could be enough for my needs but it could even be „fancier“ – using capitals in front of each cityname and different string lengths:

So I’ve added a random length variable with at least four characters and a maximum of 16:

var length = Math.floor(4 + Math.random() * 12);

And added this to my for-loop:


var possible = „abcdefghijklmnopqrstuvwxyz“;

var length = Math.floor(4 + Math.random() * 12);

var randomString = possibleCapital.charAt(Math.floor(Math.random() * possibleCapital.length));

for (var i = 0; i < length; i++) {

randomString += possible.charAt(Math.floor(Math.random() * possible.length));


Now I’ve got a nice function which will render me one single random string with a capital letter at the beginning. So all I needed to do now is to call this snippet of code a couple of times and add each string to my DataRelation output.

After adding a Scripting Relation…

Add a Scripting Relation

… in my campaign I’ve just copy & pasted the code from above to it’s editor…

Edit the data source

… and modified the section where the data is added to the „newRow“ variable:

if(Mode == „GetCount“)
    MyTable.ResultCount = 100;
else if(Mode == „LoadData“)
    var possibleCapitals = „ABCDEFGHIJKLMNOPQRSTUVWXYZ“;
    var possible = „abcdefghijklmnopqrstuvwxyz“;
    var tResult = “;
    for (var n = 0; n < 101; n++) {
        tResult = ‚NAV ‚ + possibleCapitals.charAt(Math.floor(Math.random() * possibleCapitals.length));
        var length = Math.floor(4 + Math.random() * 12);
        for (var j = 0; j < length; j++) {
            tResult += possible.charAt(Math.floor(Math.random() * possible.length));
    var newRow = MyTable.AddRow();
    newRow.SetValue(„ID“, n);
    newRow.SetValue(1, tResult);
As you can see I’ve already placed my specific prefix to the random string variable („NAV „). And finally I had my data presented:
See the result
And finally I could begin my work on the auto-completion with the nearly correct values 🙂 Challenge won!!

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

Du kommentierst mit Deinem Abmelden /  Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden /  Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden /  Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s

%d Bloggern gefällt das: