...

View Full Version : 25 dependant random numbers



goaly
05-12-2007, 12:43 PM
i am looking to kake a 5*5 grid with each square having a random number between 1 and 99. none of the numbers can be the same. i tried using a html table with a random number code in each square but this allowed more than one square of the grid to have the same number. is this possible and if so can you tell me how please?

BonRouge
05-12-2007, 01:27 PM
This seems to work: http://bonrouge.com/demos/rand_num_grid.htm


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>5x5 random number grid</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background:#fff;
}
#grid {
list-style:none;
width:260px;
margin:2em auto;
position:relative;
}
#grid li {
width:50px;
line-height:50px;
text-align:center;
border:1px solid gray;
float:left;
}
</style>
<script type="text/javascript">
function inArray(needle, haystack) {
for (h in haystack) {
if (haystack[h] == needle) {
return true;
}
}
return false;
}

window.onload=function() {
var lis=document.getElementById('grid').getElementsByTagName('li');
var nums=[];
while (nums.length<25) {
var ran=Math.ceil(Math.random()*98);
if (inArray(ran,nums)==false) {
nums.push(ran);
}
}
for (i=0; i<nums.length; i++) {
var theNum=document.createTextNode(nums[i]);
lis[i].appendChild(theNum);
}
}
</script>
</head>
<body>
<ul id="grid">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>

goaly
05-13-2007, 02:59 AM
thanks, thats perfect.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum