/* A function to change background colour to a random colour.
 *
 * 2000-09-24: Todd Owen
 * - lovingly created
 * 2000-10-02: Todd Owen
 * - modified to use csr3
 */


// I think rnd() should be used with Math.floor, not Math.ceil as
// Paul's rand() function does.
function rand2(bottom, top) {
  return Math.floor(rnd() * (top - bottom + 1)) + bottom
}


// the fabulous Constant Sum Random Triple function!  I figured
// out an algorithm on paper...I think it should work.  I'm not going
// to explain the maths here, thought.
function csr3(n) {
  var x = rand2(0, n)    // 0 <= x <= n
  var y = rand2(0, n+1)  // 0 <= y <= n+1
  if(y > n - x) {
    // (x, y) -> (n+1-y, n-x)
    var tmp = x
    x = (n + 1) - y
    y = n - tmp
  }
  var z = n - x - y
  return [x, y, z]  
}


function fillBackground() {
  var rands = csr3(10)

  var r = (0xff - rands[0]).toString(16)
  var g = (0xff - rands[1]).toString(16)
  var b = (0xff - rands[2]).toString(16)
  document.bgColor = "#" + r + g + b
}


// Most browsers about version 3.0 have Math.random in their javascript
// implementation, but I will use the following code just to be sure:


// The Central Randomizer 1.3 (C) 1997 by Paul Houle (paul@honeylocust.com)
// See:  http://www.honeylocust.com/javascript/randomizer.html

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};

// end central randomizer.


