Friday, July 28, 2006

JavaScript Random Character Generator

This is about the generation of random characters. This is just a quick little generator that I put together so I could populate a dynamic table with specific kinds of data for different columns.

This generator is included in another blog post, see the references below for more information on how I dynamically created that table.

Purpose

The purpose for this generator is to create different types of test data of different lengths. It could be modified or enhanced to fit almost any need.

The Character Types

I have kept the character types simple. There are numeric, upper and lower cased characters, and a couple mixes of these three types. No punctuation or spaces are generated. I was only need single word entities and as such, the need for spaces was not important.

If spaces are important, then new character types could be added that would generate sentence or paragraph styled blocks of random characters with spaces randomly places such that the average word size would be about 5 or 6 characters in length. Matter of fact, the punctuation that could be used could also be integrated such that different statistical benchmarking could influence which punctuation is used and where.

The Arrays

I want to discuss the purpose of the Arrays so they make a little more sense. The code is documented but if one is not going to use this as I did, then the Arrays may not mean much.

First off, where this was used was within a dynamically created table. The size could be as large as the user wants, or as small as one cell.

The issue I had, was with how to identify a consistency among the columns as far as what character types they would be and a little more consistency on their length too. The reason for this, was that I was wanting to have a table that would mimic more or less, a spreadsheet worth of data. This means that a column that has numbers, should not have any characters in it, same with a column that may represent a name, it should not have numbers interspersed on some rows.

Enter the colCharType and colMaxSize Arrays. Using these two Arrays, I can now ensure a character type uniformity that I would not be able to achieve otherwise. I will also be able to prevent wild ranges of sizes from occurring by specifying a maximum size for the number of characters. So the columns will be a bit more uniform in their appearance.

One enhancement would be to add a third Array call colMinSize which will set the minimum bounds. The reason why this would be helpful is that if you have a maximum size of 18 on a column you will still get a few rows that will contain nothing or just a few characters. So for our maximum size of 18, we may want to set a minimum size of 12. That will really help keep the columns a bit more uniformed looking and would be a great improvement.

If you have noticed, I have only defined 12 elements in both Arrays. Didn't I say that the user could enter as many columns as they like? Of course I did. I am able to get around this limitation by the use of the modulus operator in JavaScript. It is extremely useful for conditions such as these. It is used within the functions getColCharType() and getColMaxSize() where the parameter is the column number. Column 12 uses the 12th element of the Arrays and column 13 uses the first! So there will be a consistent pattern that will repeat every 12 columns.

The Source Code

I have documented the source code so it should answer most of your questions and explain what is going on. If you have any questions, please let me know.




<script type='text/javascript'>

/* Random Character Generator - Copyright 2006 by Scott Tabar - tabarATfuseDOTnet
*
* All of these functions were created from scratch, except where noted.
* I grant you the right to do what ever you wish with these.
* All I ask is that you give me credit for what I have created. If these scripts
* will be used in a product that will make you or an entity money in any shape or
* form, I just want to let you know that I don't expect a single penny of it. I
* would be honored if you will drop me a line to let me know what product it was
* used in with a reference to company name, URL, ISBN, etc.
*
* The initial purpose for the random character generators was
* to allow random text to be generated within table cells to test sorting
* performance on an arbitrary table size that could be dynamically changed without
* reloading the page. Functions and arrays have been created to be able
* to provide a reproducible data type for a given column so there will be consistency
* when a column is sorted.
*/

/* These character types are used to define what kind of random data should
* be generated for a given cell.
*/
var CHAR_TYPE_NUMERIC = 1;
var CHAR_TYPE_CHAR = 2;
var CHAR_TYPE_CHAR_UPPER = 3;
var CHAR_TYPE_CHAR_LOWER = 4;
var CHAR_TYPE_MIXED = 5;

/* This array defines what data type should be generated for the given column
*/
var colCharType = new Array(
    CHAR_TYPE_MIXED, CHAR_TYPE_MIXED, CHAR_TYPE_MIXED,
    CHAR_TYPE_CHAR_LOWER, CHAR_TYPE_CHAR_UPPER, CHAR_TYPE_CHAR,
    CHAR_TYPE_CHAR, CHAR_TYPE_CHAR, CHAR_TYPE_NUMERIC,
    CHAR_TYPE_NUMERIC, CHAR_TYPE_NUMERIC, CHAR_TYPE_NUMERIC);

/* This array defines the max number of characters that should be generated for
* the given column. With a value of 10, the column will then have from none
* to 10 characters generated.
*/
var colMaxSize = new Array( 10, 4, 4, 10, 1, 2, 18, 8, 4, 10, 10, 2 );


/* This function takes any integer number as a parameter and maps
* it to the set in the colCharType array by returning the appropriate
* value from that array as if the array repeats infinitely.
*
* This function uses the modulus function to remap the integer parameter
* to a usable value.
*/
function getColCharType( x )
{
return colCharType[ x % colCharType.length ];
}


/* This function takes any integer number as a parameter and maps
* it to the set in the colMaxSize array by returning the appropriate
* value from that array as if the array repeats infinitely.
*
* This function uses the modulus function to remap the integer parameter
* to a usable value.
*/
function getColMaxSize( x )
{
return colMaxSize[ x % colMaxSize.length ];
}

/* This function actually performs all of the generation of random data based upon
* the two parameters charType and length. Length defines the maximum length, not
* a fixed length such that if the value is 10, then result could have a length of
* zero or anything up to a length of 10 characters.
*
* Currently, no character type generates punctuation or white spaces. It is
* intended to generate simple single "word" entities.
*/
function rndText( charType, length )
{
var result = "";

for( var x = 0; x < length; x++ )
{
var cType = charType;

if ( cType == CHAR_TYPE_MIXED ) {
cType = (Math.round(Math.random() * 3) + 1);
}
if ( cType == CHAR_TYPE_CHAR ) {
cType = (Math.round(Math.random() * 1) + 3);
}

if ( cType == CHAR_TYPE_NUMERIC ) {
result += createRandomNumbers();
} else
if ( cType == CHAR_TYPE_CHAR_UPPER ) {
result += createRandomUpperCaseLetter();
} else
if ( cType == CHAR_TYPE_CHAR_LOWER ) {
result += createRandomLowerCaseLetter();
}
}
return result;
}

/* Returns a random number between zero and the numeric value of the parameter.
*/
function rndNumMax( maxNum )
{
return Math.round(Math.random() * maxNum);
}

/* Returns one character that is randomly selected between 0 and 9.
*
* Thanks to wwww.CodeHouse.com for the following inspiration:
* http://www.codehouse.com/javascript/tips/random_letter/
*/
function createRandomNumbers()
{
return String.fromCharCode(48 + Math.round(Math.random() * 9));
}

/* Returns one character that is randomly selected between a and z.
*
* Thanks to wwww.CodeHouse.com for the following inspiration:
* http://www.codehouse.com/javascript/tips/random_letter/
*/
function createRandomLowerCaseLetter()
{
return String.fromCharCode(97 + Math.round(Math.random() * 25));
}

/* Returns one character that is randomly selected between a and Z.
*
* Thanks to wwww.CodeHouse.com for the following inspiration:
* http://www.codehouse.com/javascript/tips/random_letter/
*/
function createRandomUpperCaseLetter()
{
return String.fromCharCode(65 + Math.round(Math.random() * 25));
}

</script>




References

Dynamic Table Generation - Where this rnd char generation is at work!

Creating random characters

No comments: