Mega Code Archive

 
Categories / JavaScript DHTML / Language Basics
 

Unique Random Numbers

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Unique Random Numbers</title> <style type="text/css"> .head{font-family:verdana,arial,helvetica; font-weight:bold; font-size:20pt; color:#FF9900; filter:DropShadow(color=#000000, offX=2, offY=2, positive=1); width:100%} .link{font-family:verdana,arial,helvetica; font-size:10pt; color:#000000} .link:hover{font-family:verdana,arial,helvetica; font-size:10pt; color:#FF9900} </style> <!--BEGIN HEAD SECTION CODE--> <script language="JavaScript"> // Unique Random Numbers // -Picks a number of unique random numbers from an array // (c) 2002 Premshree Pillai // http://www.qiksearch.com // http://premshree.resource-locator.com // E-mail : qiksearch@rediffmail.com function pickNums(nums, numArr) {   if(nums>numArr.length)   {     alert('You are trying to pick more elements from the array than it has!');     return false;   }   var pickArr=new Array();   var tempArr=numArr;   for(var i=0; i<nums; i++)   {     pickArr[pickArr.length]=tempArr[Math.round((tempArr.length-1)*Math.random())];     var temp=pickArr[pickArr.length-1];     for(var j=0; j<tempArr.length; j++)     {       if(tempArr[j]==temp)       {         tempArr[j]=null;         var tempArr2=new Array();         for(var k=0; k<tempArr.length; k++)           if(tempArr[k]!=null)             tempArr2[tempArr2.length]=tempArr[k];         tempArr=tempArr2;         break;       }     }   }   return pickArr; }     </script> <!--END HEAD SECTION CODE--> </head> <body bgcolor="#FFFFFF"> <center><span class="head">Unique Random Numbers</span></center> <br> <table width="400" align="center"><tr><td> <font face="verdana,arial,helvetica" size="-1" color="#000000"> This JavaScript picks up a number of unique random elements from an array. <br><br>For example; if you have an array <font face="courier">myArray</font> consisting of 10 elements and want to pick 5 unique random elements. Suppose initially <font face="courier">myArray[3]</font> is picked randomly, then <font face="courier">myArray[3]</font> should not be picked again. <br><br>If you want to pick 4 numbers, call the function like this : <br><font face="courier">pickNums(4,myArray)</font>. This function will return an array consisting of 4 unique random numbers. Thus you can store this array like this :<br><font face="courier">var anotherArray=pickNums(4,myArray)</font>.<br>You can now use this array for displaying the elements in different formats. <br><br>Could be useful :-) <br><br>Here's an example : <!--BEGIN BODY SECTION CODE--> <script language="JavaScript"> /* Add elements to this array */ var myArr = new Array("1","2","3","4","5","6","7","8","9"); var outArr=pickNums(5, myArr); /* Store the output */ /* Print Output */ /* Modify this part to suit your output needs */ document.write('<span style="background:#FFFFFF; border:#000000 solid 1px; padding:2px">'); for(var i=0; i<outArr.length; i++) {   document.write('<b>' + outArr[i] + '</b> '); } document.write('</span>'); </script> <!--END BODY SECTION CODE--> <br><br><a href="javascript:void(location.reload())" class="link">Reload</a> the page to see a set of another unique random numbers. <hr color="#808080"> &#169; 2002 <a href="http://www.qiksearch.com" class="link">Premshree Pillai</a>. </font> </td></tr></table> </body> </html>