You are here: irt.org | Articles | JavaScript | Text, String, and Number | Selecting Random Numbers [ previous next ]
Published on: Saturday 29th November 1997 By: Martin Webb
<HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"><!-- function makeArray() { this[0] = makeArray.arguments.length; for (i = 0; i<makeArray.arguments.length; i++) this[i+1] = makeArray.arguments[i]; } var colors = new makeArray('aliceblue','antiquewhite','aqua','aquamarine','azure', 'beige','blanchedalmond','blue','blueviolet','brown','burlywood', 'cadetblue','chartreuse','chocolate','coral','cornflowerblue', 'cornsilk','crimson','cyan','darkblue','darkcyan','darkgoldenrod', 'darkgray','darkgreen','darkkhaki','darkmagenta','darkolivegreen', 'darkorange','darkorchid','darkred','darksalmon','darkseagreen', 'darkslateblue','darkslategray','darkturquoise','darkviolet', 'deeppink','deepskyblue','dimgray','dodgerblue','floralwhite', 'forestgreen','fuchsia','gainsboro','ghostwhite','gold','goldenrod', 'gray','green','greenyellow','honeydew','hotpink','indianred', 'ivory','khaki','lavender','lavenderblush','lawngreen', 'lemonchiffon','lightblue','lightblue','lightcoral','lightcyan', 'lightgoldenrodyellow','lightgreen','lightgrey','lightpink', 'lightsalmon','lightseagreen','lightskyblue','lightslategray', 'lightsteelblue','lightyellow','lime','limegreen','linen','magenta', 'maroon','mediumaquamarine','mediumblue','mediumorchid', 'mediumpurple','mediumseagreen','mediumslateblue', 'mediumspringgreen','mediumturquoise','mediumvioletred', 'midnightblue','mintcream','mistyrose','moccasin','navajowhite', 'navy','oldlace','olive','olivedrab','orange','orangered','orchid', 'palegoldenrod','palegreen','paleturquoise','palevioletred', 'papayawhip','peachpuff','peru','pink','plum','powderblue','purple', 'red','rosybrown','royalblue','saddlebrown','salmon','sandybrown', 'seagreen','seashell','sienna','silver','skyblue','slateblue', 'slategray','snow','springgreen','steelblue','tan','teal','thistle', 'tomato','turquoise','violet','wheat','white','whitesmoke','yellow', 'yellowgreen'); function MakeEmptyArray(size) { this[0] = size; for (i = 1; i <= size; i++) this[i] = ''; } function ResetLotteryBalls() { for (var i = 1; i <= lotterynumbers[0]; i++) lotterynumbers[i] = ''; } function SelectLotteryBalls(number) { while (number > 0) { random = rand(lotterynumbers[0]); if (lotterynumbers[random] == '') { lotterynumbers[random] = 'X'; number--; } } } function ShowLotteryBalls() { document.write('<P>'); for (var i=1; i <= lotterynumbers[0]; i++) { if (lotterynumbers[i] != '') document.write(' <IMG SRC="balls/'+i+'.gif" HEIGHT=50 WIDTH=50 ALT="' + i +'"> '); } } //--></SCRIPT> <SCRIPT> <!-- // The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu) // See: http://www.msc.cornell.edu/~houle/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. --> </SCRIPT> </HEAD> <BODY><CENTER> <SCRIPT LANGUAGE="JavaScript"><!-- var lotteryballs = 49; var ballstoselect = 6; var lotterynumbers = new MakeEmptyArray(lotteryballs); document.write('A selection of random lottery numbers<BR>'); var color = colors[rand(colors[0])]; document.write('<TABLE BGCOLOR="' + color + '"><TR><TD ALIGN="CENTER" HEIGHT="100">'); ResetLotteryBalls();SelectLotteryBalls(ballstoselect);ShowLotteryBalls(); document.write('<\/TD><\/TD><\/TABLE>'); //--></SCRIPT> <A HREF="index.htm" TARGET="_parent">Return</A> </CENTER></BODY> </HTML>
How long is a piece of string?