May 23,1998 The central randomizer was sorely needed when I wrote it, and it was quite popular for a while and was even used by a very large enterprise software company. These days most people have a browser that supports Math.random() so it's less important than it was in the past but we think it can still come in handy for somebody.

The Central Randomizer

Warning: Although Netscape 2.0 is now two versions old and pretty much on it's way out, there is a javascript gotcha that you still ought to look out for. Netscape 2.0 can crash if you have javascripts in a page where images do not have their heights and widths specified. If your javascript uses document.write() to insert an unsized image, Netscape 2.0 will crash. This problem does not affect NS 3.0 or 4.0 or IE 3.0. Just size your images when you insert a random one and everything will work out OK.

If your browser supports Javascript such as Netscape 3.0 you will see 10 random floating point numbers as well as 10 random integers ranging from 1 to 10.

I wrote this back in December 1995 when I found out that Math.random() didn't work in most versions of Netscape 2.0. The Central Randomizer is a linear congruential random number generator with coefficients from Numerical Recipes, automatically seeded from your system clock.

The Central Randomizer is a good source of mildly random numbers that are quite likely to give you different random numbers every time. This isn't a research grade random number generator and I wouldn't use it to keep secrets, but it's good for many uses. Reload for a fresh batch of random numbers. The central randomizer has a big brother. I've written a high-speed research-grade random number package in Java.



How to use the central randomizer

Here's the source code for the random number generator, which is inserted in the head of this HTML document. It seeds itself and defines functions that can be used in the rest of the document. The latest version (1.3) is streamlined, and minimizes the possibility of namespace colisions. I also added code to hide the script from non-javascript browsers.

Insert the following into the <HEAD> of your document.


<SCRIPT LANGUAGE="JavaScript">
<!--
// 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. -->
As an example here's the code that makes the table. It's inserted into this document like so
<SCRIPT LANGUAGE="JavaScript">

document.write("<CENTER>");
document.write('<TABLE BORDER CELLPADDING=5>');
document.write("<TR><TD><CENTER>Real</CENTER></TD><TD>Integer</TD></TR>");
for(i=1;i<=10;i++) {
	document.write("<TR><TD><CODE>");
	document.write(rnd());
	document.write("</CODE></TD><TD><CENTER>");
	document.write(rand(10));
	document.write("</CENTER></TD></TR>");
};

document.write("</TABLE></CENTER>");

</SCRIPT>

Feel free to use the Central Randomizer on your page. I do ask that you keep the comment intact; it also would be nice if you were to drop a link to one of my pages.


Copyright © 1997 honeylocust media systems, contact paul@honeylocust.com