Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    25 dependant random numbers

    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?

  • #2
    Regular Coder BonRouge's Avatar
    Join Date
    Mar 2006
    Location
    Sendai, Japan
    Posts
    328
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This seems to work: http://bonrouge.com/demos/rand_num_grid.htm

    Code:
    <!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>

  • #3
    New to the CF scene
    Join Date
    May 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, thats perfect.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •