<HTML><HEAD>
<!--
-----------------
Random Numbers #3
-----------------
-->
<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers
/*
THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
Copyright (c)1998 by Charles River Media. All Rights Reserved.
This applet can only be re-used or modifed by license holders of the
JavaScript Cookbook CD-ROM. Credit must be given in the source
code and this copyright notice must be maintained. If you do
not hold a license to the JavaScript Cookbook, you may NOT
duplicate or modify this code for your own use.
Use at your own risk. No warranty is given or implied of the suitability
of this applet for any specific application. Neither Erica Sadun nor
Charles River Media will be held responsible for any unwanted effects
due to the use of this applet or any derivative.
*/
//------------------ARRAY CREATION----------------------------
// create an array
function createArray(n)
{
for (var i = 0; i < n; i++) {this[i] = 0}
return this
}
//------------------RANDOM NUMBERS----------------------------
/* This random function inspired by _Numerical Recipes in C_ by
Press, Flannery, Teukolsky and Vetterling */
var JC_M = 714025
var JC_IA = 1366
var JC_IC = 150889
var jcRand = 0
var jcir = 0
// Next shuffle index
function kick(n){return (JC_IA * n + JC_IC) % JC_M}
// Initialize the shuffle table
function srand()
{
var now = new Date()
jcRand = now.getHours() + now.getMinutes() * 60 + now.getSeconds() * 3600
jcir = new createArray(98)
jcRand = (JC_IC + jcRand) % JC_M;
for (var j = 0; j <= 97; j++)
{
jcRand = kick(jcRand)
jcir[j] = jcRand;
}
jcRand = kick(jcRand)
}
// Returns a random deviate between 0.0 and 1.0
function rand()
{
// find location in shuffle table
j = Math.round(1 + 97.0 * jcRand / JC_M);
if (j < 1) {j = 1}
if (j > 97) {j = 97}
// adjust index and return deviate
var tmp = jcir[j]
jcRand = kick(jcRand)
jcir[j]= jcRand
return tmp/JC_M
}
// Return a Random Float between 0 and N-1
function randomFloat(N) {return (rand() * N)}
// Return a Random Integer between 0 and N-1
function random(N) {return (Math.floor(rand() * N))}
//------------------HISTOGRAM----------------------------
// Histogram shown as stars
function histo1(n)
{
for (var hi = 0; hi < n; hi++)
{document.write("*")}
document.writeln("<br>")
}
<!-- done hiding --></SCRIPT></HEAD>
<BODY bgcolor="ffffff" link="0000ff" vlink="770077">
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
ALIGN = LEFT>Better Random Numbers</H1></FONT>
<BLOCKQUOTE>
<FONT SIZE=4>
This script creates a distribution of 100 numbers
between 0 and 9 and prints their histograms using
a better, but larger, random number generator than
that used in previous examples. "Random"
numbers created with formulas are not truly random.
They are, in fact, entirely predictable because they
are generated by formula. For most purposes, however, a
good number generator simulates randomness over a broad class
of applications. If your script needs many
statistically valid random numbers for simulation, consider using
this generator rather than the simpler one used in the
two previous examples.
</FONT>
<BR><BR>
<BLOCKQUOTE><FONT COLOR="770000"><SCRIPT>
var ra = new createArray(10)
srand()
for (var xi = 0; xi < 100; xi++)
{
var x = random(10)
ra[x]++
}
for (var xi = 0; xi < 10; xi++)
{
document.write(""+xi+": ")
histo1(ra[xi])
}
</SCRIPT></FONT>
<FORM>
<INPUT TYPE="button" VALUE="RE-ROLL and RELOAD"
onClick="history.go(0)">
</FORM>
</BLOCKQUOTE></BLOCKQUOTE>
<h5>Copyright ©1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>