1 /** 2 * @fileOverview Contains the code for generating a random matrix. 3 * @version 0.1 4 * @author Pulkit Goyal <pulkit110@gmail.com> 5 */ 6 /** 7 * Generates random matrix with characters from A-F and numbers. 8 * 9 * @class MatrixGenerator 10 */ 11 function MatrixGenerator(type) { 12 var that = this; 13 14 var STR_TYPE_RANDOM = 'random'; 15 var STR_TYPE_SEQUENTIAL = 'sequential'; 16 17 if (type == undefined) { 18 type = 'random'; 19 } 20 21 var INT_MAX_LABEL_LENGTH = 4; 22 var FLO_NUM_PROBABILITY = 0.5; 23 24 var arr_stateLabels = ['A', 'B', 'C', 'D', 'E', 'F']; 25 26 var mat_testMatrix; 27 var arr_rowLabels; 28 var arr_colLabels; 29 30 /** 31 * Returns a random string of given length. 32 * @param {Number} length - length of the string. 33 * @returns {string} random string of given length. 34 */ 35 function getRandomString(length) { 36 var str_randomString = ''; 37 for (var i = 0; i < length; ++i) { 38 str_randomString += (Math.random() > FLO_NUM_PROBABILITY)?arr_stateLabels[Math.floor(Math.random()*arr_stateLabels.length)]:Math.floor(Math.random()*10); 39 } 40 return str_randomString; 41 }; 42 43 /** 44 * Initialize the MatrixGenerator. 45 * @param {Number} int_height - height of the matrix to generate. 46 * @param {Number} int_width - width of the matrix to generate. 47 */ 48 this.init = function(int_height, int_width) { 49 mat_testMatrix = []; 50 arr_rowLabels = []; 51 arr_colLabels = []; 52 53 for (var i = 0; i < int_height; ++i) { 54 if (type == STR_TYPE_RANDOM) { 55 arr_rowLabels.push(getRandomString(Math.random()*INT_MAX_LABEL_LENGTH)); 56 } else { 57 arr_rowLabels.push('row ' + i); 58 } 59 60 mat_testMatrix.push([]); 61 for (var j = 0; j < int_width; ++j) { 62 if (type == STR_TYPE_RANDOM) { 63 if (i == 0) 64 arr_colLabels.push(getRandomString(Math.random()*INT_MAX_LABEL_LENGTH)); 65 mat_testMatrix[i].push(getRandomString(Math.random()*INT_MAX_LABEL_LENGTH)); 66 } else { 67 if (i == 0) 68 arr_colLabels.push('col ' + j); 69 mat_testMatrix[i].push(i + ', ' + j); 70 } 71 72 } 73 } 74 }; 75 76 /** 77 * Returns the complete matrix. 78 * @returns {ArrayOfArray} mat_testMatrix - An array of array representing 79 * the matrix. 80 */ 81 this.test_getMatrix = function() { 82 return mat_testMatrix; 83 }; 84 85 /** 86 * Returns the complete matrix. 87 * @returns {Array} arr_rowLabels - An array of row labels. 88 */ 89 this.test_getMatrixRowLabels = function() { 90 return arr_rowLabels; 91 }; 92 93 /** 94 * Returns the complete matrix. 95 * @returns {Array} arr_rowLabels - An array of column labels. 96 */ 97 this.test_getMatrixColumnLabels = function() { 98 return arr_colLabels; 99 }; 100 };