var ustrn = 0;
var UserChoiceNumbersUnique = false;
var UserChoiceSortListItems = false;
var UserSortOrder = "Ascending";

var i;
 
var genRandNo;    // Stores Generated random number.
var uslrn = 0;    // Stores User Selected Lower Range Number.
var usurn = 0;    // Stores User Selected Upper Range Number.
  
function NextRandomNumber() {
  var hi   = this.seed / this.Q;
  var lo   = this.seed % this.Q;
  var test = this.A * lo - this.R * hi;
  if (test > 0)
    this.seed = test;
  else
    this.seed = test + this.M;
  return (this.seed * this.oneOverM);
}
 
function RandomNumberGenerator() {
  var d = new Date();
  this.seed = 2345678901 +
    (d.getSeconds() * 0xFFFFFF) +
    (d.getMinutes() * 0xFFFF);
  this.A = 48271;
  this.M = 2147483647;
  this.Q = this.M / this.A;
  this.R = this.M % this.A;
  this.oneOverM = 1.0 / this.M;
  this.next = NextRandomNumber;
  return this;
}
 
function random(lrn, urn) {

  // Return a generated random number (Integer from 'LowerRangeNumber' and up to 'UpperRangeNumber'; both numbers included).
  // Random LowerRange Number (lrn).
  // Random UpperRange Number (urn).
  // return Math.round((urn - lrn + 1) * rand.next() + lrn);
  return Math.floor((urn - lrn + 1) * rand.next() + lrn);

}
 
var rand = new RandomNumberGenerator();  // Implement the RandomNumberGenerator (RNG) as an object.

genRandNo = random(uslrn, usurn);  // Generate random number (Integer from 'LowerRangeNumber' and up to 'UpperRangeNumber'; both numbers included).
 
function doAddItemToDelimitedListStr(targetItemListStr, addItemStr, addDelimiterStr) {

  if( (targetItemListStr == null) || (targetItemListStr == "") || (targetItemListStr.length == 0) ){
    targetItemListStr = "";
  }
  targetItemListStr = "" + targetItemListStr;  // Force number to a string.
  // Make sure of the variable value if 'nothing' is the contents of the input.
  if( (addItemStr == null) || (addItemStr == "") || (addItemStr.length == 0) ){
    addItemStr = "";
  }
  addItemStr = "" + addItemStr;  // Force number to a string.
  // Make sure of the variable value if 'nothing' is the contents of the input.
  if( (addDelimiterStr == null) || (addDelimiterStr == "") || (addDelimiterStr.length == 0) ){
    addDelimiterStr = "";
  }
  addDelimiterStr = "" + addDelimiterStr;  // Force number to a string.
  if( targetItemListStr.length >= 1 ){
    // There is content in the string (Add delimiter (separator) text string or character and add new item text string to text string).
    targetItemListStr += addDelimiterStr + addItemStr;
  } else {
    // There is no content in the string.
    if( addItemStr.length == 0 ){
      // There is no content in the string and there is no content in the new item string (Add only delimiter (separator) text string or character to text string).
      targetItemListStr = "" + addDelimiterStr;
    } else {
      // There is no content in the string but there is content in the new item string (Add only new item text string to text string).
      targetItemListStr = "" + addItemStr;
    }
  }
  return targetItemListStr;
}
 
function MakeArray(n) {
  // array initialization function
  this.length = n;
  for (i=1; i<=n; i++) {
    this[i] = 0;
  }
  return this;
}
 
function setValues(a, b) {

  uslrn = a;

  usurn = b;

  ustrn = 2;

  uslrn = parseInt(uslrn, 10);  // Force string to a number.
  usurn = parseInt(usurn, 10);  // Force string to a number.
  ustrn = parseInt(ustrn, 10);  // Force string to a number.

  UserChoiceNumbersUnique = true;   // Store User Choice Numbers Unique - Boolean value.
  UserChoiceSortListItems = false;  // Store User Choice Sort List Items - Boolean value.
  UserSortOrder = "Ascending";            // Sort order selected by user specifying the sort criteria 
                                          //   'Sort Ascending [A-Z]' (Ascending) or 'Sort Descending [Z-A]' 
                                          //   (Descending) [Default: Ascending].
  return;

}
 
function calculate() {

  setValues(1, 4);

  var delimitedString = "";           // Stores delimited text string (String standard [default] delimited - only one separator character used).
  var specialDelimitedString = "";    // Stores special delimited text string (String optimized for search of items).
  var formattedDelimitedString = "";  // Stores formatted delimited text string (String formatted for output).
  var sepChr = ",";                   // Separator Character (Delimiter Character).
  var sepStr = ",";                   // Separator String.
  var itemAlreadyInList = false;      // Stores state for item already in list - Boolean value.
  var itemsInListInt = 0;             // Stores count of item(s) in list - Integer.
  var genRandNoSumTotal = 0;          // Generated random number(s): Sum total.
  var genRandNoStr = "";              // Stores Generated random number - String.

  while (itemsInListInt < ustrn) {

    genRandNo = random(uslrn, usurn);
    genRandNoStr = "" + genRandNo;

    if(UserChoiceNumbersUnique == true ) {

      itemAlreadyInList = ( specialDelimitedString.indexOf(("$" + genRandNoStr + "$"),0) != -1 ) ? true : false;
      if(itemAlreadyInList == true) {

      } else {

        delimitedString = doAddItemToDelimitedListStr(delimitedString, genRandNoStr, sepChr);
        specialDelimitedString = doAddItemToDelimitedListStr(specialDelimitedString, ("$" + genRandNoStr + "$"), sepChr);
        formattedDelimitedString = doAddItemToDelimitedListStr(formattedDelimitedString, genRandNoStr, sepStr);
        itemsInListInt += 1;

      }

    }

  }

  //alert(formattedDelimitedString);
  return formattedDelimitedString;

}
