### 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.

```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;
```var getRandomColor = function() {