<HTML><HEAD>
<!--
-----------------
Random Numbers #1
-----------------
-->
<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.
*/
//------------------RANDOM NUMBERS----------------------------
var js_mult1=3141
var js_mult2=5821
var js_m1=100000000
var js_m2=10000
var js_iseed=0
var js_iseed1=0
var js_iseed2=0
// Return a Random Integer between 0 and N-1
function random(n)
{
if (js_iseed == 0)
{
now = new Date()
js_iseed = now.getHours() + now.getMinutes() * 60
+ now.getSeconds() * 3600
}
js_iseed1 = js_iseed / js_m2
js_iseed2 = js_iseed % js_m2
var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) *
js_m2 + (js_iseed2 * js_mult2)) % js_m1
js_iseed = (tmp + 1) % js_m1
return (Math.floor((js_iseed/js_m1) * n))
}
//------------------ARRAY CREATION----------------------------
// create an array
function createArray(n)
{
for (var i = 0; i < n; i++) {this[i] = 0}
return this
}
//------------------HISTOGRAM----------------------------
function histo(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 = CENTER>Random Numbers</H1></FONT>
<BLOCKQUOTE><FONT SIZE=4>
This script creates a distribution of 100 numbers
between 0 and 9 and prints their histogram. Keep
reloading to see many distributions. You can reuse
this random number generator in your applications and
games.
</FONT>
<BR><BR>
<BLOCKQUOTE><FONT COLOR="770000"><SCRIPT>
var ra = new createArray(10)
for (var xi = 0; xi < 100; xi++)
{
var x = random(10)
ra[x]++
}
for (var xi = 0; xi < 10; xi++)
{
document.write(""+xi+": ")
histo(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>