Applications of Random

I do not find myself needing random numbers very often in my projects, so I thought it would be good to talk about a few useful applications of Random Integers.

The easily to implement without any additional programming would be a dice roller. You need only call Math.RandomInteger(n), where n is the number of sides of the die you wish to roll. So a six-sided Function might look like:

Example 1: Six-Sided Die

var d6 = function(mod) {
	if (! mod) {mod = 0;}
	return Math.RandomInteger(6) + mod;
}

I made a slight modification to the Function, so that you can modify the result if you desire. Another example are casino games. Most likely they will use a stronger Random Function than the one I have provided, but the concept is the same. Here is a simple shuffle Function for a deck of cards:

Example 2: Card Shuffle

var deck = [
H1, H2, H3, H4, H5, H6, H7,
H8, H9, H10, H11, H12, H13,
D1, D2, D3, D4, D5, D6, D7,
D8, D9, D10, D11, D12, D13,
S1, S2, S3, S4, S5, S6, S7,
S8, S9, S10, S11, S12, S13,
C1, C2, C3, C4, C5, C6, C7,
C8, C9, C10, C11, C12, C13
];

var shuffle = function(n) {
	for (var i = n - 1; 0 <= i; i -= 1) {
		var r1 = Math.RandomInteger(deck.length) - 1;
		var r2 = Math.RandomInteger(deck.length) - 1;
		var temp = deck[r1];
		deck[r1] = deck[r2];
		deck[r2] = temp;
	}
}

First we create a deck of cards, where each card is represented by a string containing the first letter of the suit and the face-value of the cards, so: H = hearts, D = diamonds, S = spades, C = clubs, 1 = ace, 11 = jack, 12 = queen, and 13 = king. To randomize the order of these cards, the shuffle Function will randomly swap the position of two cards n number of times. The higher n is the more random the cards in the deck will be organized.

The last example for today is a hexadecimal number generator, which will be used to generate random colors. I used this on the Graph Beta Test Page to change the colors of the lines and legends when adding additional lines to the graph.

Example 3: Random Hexadecimal

Number.prototype.toHexadecimal = function() {
	return this.toString(16);
}

String.prototype.todecimal = function() {
	return parseInt(this.replace(/[^0-9A-Fa-f]/g, ), 16);
}

var getRandomHex = function(n) {
	if (! n) {n = 16;}
	var m = Math.RandomInteger(n) - 1;
	return m.toHexadecimal();
};

The first method appends to the Number prototype a method that converts a Number into a hexadecimal String, using native JavaScript method toString. Unfortunately, native toString method does not preserve significant figures and a call like "(256).toHexadecimal()" can return A or 1, instead of 0A or 01. The second method does the reverse, converting a hexadecimal String into a Number, using the native parseInt method with a base 16. We also strip out any characters that arent valid hexadecimal characters before we attempt to parse the integer. Lastly, we can leverage the toHexadecimal method to convert a random integer (0, 15) into a hexadecimal in the getRandomHex method. That method also accepts a Number n in case you want to generate a random hexadecimal that is larger than 16.

Using getRandomHex we can now generate random colors:

Example 4: Random Colors

var getRandomColor = function() {
	return # + getRandomHex() + getRandomHex() + getRandomHex() + getRandomHex() + getRandomHex() + getRandomHex();
};

To give these examples a try, I have created a simple Random Applications Test Page.