Random numbers in JavaScript

Published 07 May 07 03:28 PM | despos

The Microsoft AJAX Client library serves you a so rich JavaScript language that it doesn't even look like a script language. In this regard, it makes it almost natural to think new functionalities in terms of classes. For example, random numbers. Here's a quick class to mimick the behavior of the managed Random class:

//
// Samples.Random ::
//     Generates random numbers
//

// Define the namespace of the class
Type.registerNamespace('Samples');

// Constructor
Samples.Random = function Samples$Random()
{
    Samples.Random.initializeBase(this);
}

// Method to generate a random number
function Samples$Random$getNumber(minNumber, maxNumber) {
    var num = minNumber + Math.floor(Math.random() * maxNumber);
    return num;
}

// Class prototype
Samples.Random.prototype =
{
    getNumber:  Samples$Random$getNumber
}

// Register the new class
Samples.Random.registerClass('Samples.Random');

// Get a static instance of the class
Samples.Random._staticInstance = new Samples.Random();


// Define static global members
Samples.Random.getNumber = function(minNumber, maxNumber)
{
   return Samples.Random._staticInstance.getNumber(minNumber, maxNumber);
}

Basically, you go through the following steps:

  1. Declare the namespace you want the class to fall in.
  2. Add the class constructor. It's actually a function as there's no class keyword in JS, so you might want to look at it as a function that initializes the "class". By using the initializeBase method you can invoke the constructor on any base class.
  3. Define as many members as needed to implement methods and properties. Note that a property always needs a pair of get/set methods. Ideally, you define "internal" function for each of the members you plan to expose. You can create these functions as anonymous or named. Named functions are preferable because it helps the VS "Orcas" debugger.
  4. Define the prototype of the class--that is the "official" list of members of the class. In the prototype, you bind public names to internal functions/properties.
  5. Add the class to the AJAX library. At this time, you can specify any base class and any interface the class implements.
  6. You're done. Unless, you want to make the class operate as a singleton. (Just for performance reasons.) In this case, you create the single instance of the class and list static members to be invoked on that.

Everything in the prototype can be extended in derived classes.

NOTE:: I've used this class in my Cutting Edge column, July 2007, for MSDN Magazine.
NOTE:: This is NOT the ONLY way of creating new objects in JS. This is only the prototype-model as implemented and recommended by the MS AJAX library.

Filed under:

Comments

# Rob van der Woude said on August 11, 2007 08:00 AM:

Isn't that supposed to be:

var num = minNumber + Math.floor(Math.random() * (maxNumber - minNumber + 1));

Leave a Comment

(required) 
(required) 
(optional)
(required)