//	cRandomizedStack Javascript - Copyright (C) 2007-2010 Charles A Upsdell, All Rights Reserved; www.upsdell.com
//
aModule.has ( 'cRandom' );


// CLASSES

// cRandomizedStack Class Constructor
function cRandomizedStack ( arg1, arg2 )
{
switch ( arguments.length )
  {
  case	0:
		this.start = 0;			// Start (low value) of stack of integers
		this.length = 52;		// Length of stack
		break;
  case	1:
		if ( arg1 < 1 )
			alert( 'cRandomizedStack() error, length < 1' );
		this.start = 0;
		this.length = arg1;
		break;
  case	2:
		if ( arg1 >= arg2 )
			alert( 'cRandomizedStack() error, start >= end' );
		this.start = arg1;
		this.length = arg2 - arg1 + 1;
		break;
  default:
		alert( 'cRandomizedStack() error, too many arguments' );
		break;
  }
this.index = this.length;	// Index into stack
this.stack = new Array();	// Stack
return;
}

// Method to randomize the stack in a cRandomizedStack object
cRandomizedStack.prototype.randomize = function ()
{
var i, j, randomInteger;
for ( i = 0; i < this.length; ++i )
	this.stack[i] = i;
for ( i = 0; i < this.length; ++i )
  {
	j = i + Math.floor( (this.length-i)*Math.random() );
	randomInteger = this.stack[j];
	this.stack[j] = this.stack[i];
	this.stack[i] = randomInteger;
  }
this.index = 0;
return;
}

// Method to draw a random number from cRandomizedStack object
cRandomizedStack.prototype.draw = function ( option )
{
if ( this.length <= this.index )
  {
	if ( arguments.length > 0 )
		return option;
	else
	  {
		this.randomize();
		this.index = 0;
	  }
  }
var value = this.stack[this.index++];
return value + this.start;
}


