Unique Random Sets : Random : Language Basics JAVASCRIPT DHTML TUTORIALS


JAVASCRIPT DHTML TUTORIALS » Language Basics » Random »

 

Unique Random Sets



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Unique Random Sets</title>
<!--BEGIN HEAD SECTION CODE-->
<script language="JavaScript">
// Unique Random Sets Picker
// Based on : Unique Random Numbers
// -http://www.qiksearch.com/javascripts/random_numbers1.htm
// -Picks a number of unique random numbers from an array
// (c) 2002 Premshree Pillai
// http://www.qiksearch.com, http://javascript.qik.cjb.net
// E-mail : qiksearch@rediffmail.com

function pickNums(nums, numArr, pickArr, count, doFlag, iterations)
{
 iterations+=1;
 var currNum = Math.round((numArr.length-1)*Math.random());
 if(count!=0)
 {
  for(var i=0; i<pickArr.length; i++)
  {
   if(numArr[currNum]==pickArr[i])
   {
    doFlag=true;
    break;
   }
  }
 }
 if(!doFlag)
 {
  pickArr[count]=numArr[currNum]// We create the array for use later
  count+=1;
 }
 if(iterations<(numArr.length*3)) // Compare for max iterations you want
 {
  if((count<nums))
  {
   pickNums(nums, numArr, pickArr, count, doFlag, iterations);
  }
 }
 else
 {
  location.reload();
 }
}
</script>
<!--END HEAD SECTION CODE-->
</head>
<body bgcolor="#FFFFFF">
<center><span style="font-family:verdana,arial,helvetica; font-weight:bold; font-size:19pt; color:#3366CC; filter:DropShadow(color=#C0C0CC, offX=2, offY=2, positive=1); width:100%">Unique Random Sets</span></center>
<br>

<table width="400" align="center"><tr><td>
<font face="verdana,arial,helvetica" size="-1" color="#000000">
This JavaScript, based on <a href="http://www.qiksearch.com/javascripts/urn20.htm"><font color="#3366CC">Unique Random Numbers II</font></a>, picks Unique Random Sets.
<br><br>Here, there are more than one arrays (need not be of same length). First, we pick a number of unique random elements from the first array, <font face="courier">numArr1</font> to form the array <font face="courier">pickArr1</font>. Now each element of <font face="courier">pickArr1</font> picks another element from the second array <font face="courier">numArr2</font>, which again is unique and random. Thus we form the array <font face="courier">pickArr2</font>. Similarly each element of <font face="courier">pickArr2</font> picks unique random elements from <font face="courier">numArr3</font> to form <font face="courier">pickArr3</font>. 

<br>i.e if the first element of <font face="courier">pickArr1</font> picks the second element of <font face="courier">numArr2</font>, then no other element of <font face="courier">pickArr1</font> should pick the second element of <font face="courier">numArr2</font> again.
<br><br>In effect what we are doing is picking equal number of unique random numbers from a number of arrays(In this exaple, arraysto form a number of unique random sets, containing number of elements same as the number of arrays.
<br><br>Here's an example :
<br><br>
<table style="background:#FAFAFF; border:#F0F0FF solid 2px" cellspacing="5" cellpadding="5"><tr><td>
<!--BEGIN BODY SECTION CODE-->

<script language="JavaScript">
// (c) 2002 Premshree Pillai
// http://www.qiksearch.com, http://javascript.qik.cjb.net
// E-mail : qiksearch@rediffmail.com

//---------The First Array---------
var numArr1 = new Array("0","1","2","3","4","5","6","7","8","9")// Add elements here
var pickArr1 = new Array()// The array that will be formed
var count1=0;
var doFlag1=false;
var iterations1=0;
pickNums(5, numArr1, pickArr1, count1, doFlag1, iterations1);

//---------The Second Array---------
var numArr2 = new Array("10","11","12","13","14","15","16","17","18","19")// Add elements here
var pickArr2 = new Array()// The array that will be formed
var count2=0;
var doFlag2=false;
var iterations2=0;
pickNums(5, numArr2, pickArr2, count2, doFlag2, iterations2);

//---------The Third Array---------
var numArr3 = new Array("20","21","22","23","24","25","26","27","28","29")// Add elements here
var pickArr3 = new Array()// The array that will be formed
var count3=0;
var doFlag3=false;
var iterations3=0;
pickNums(5, numArr3, pickArr3, count3, doFlag3, iterations3);

/* 
   To add more arrays, copy the array block, like the one above.
   Just modify the variable names. For example for a fourth array,
   copy the third array block, change the variable names numArr3
   to numArr4. Similarly, modify the name of all variables. 
   You will also have to modify the writeGen() function below to
   include more arrays.
*/

// This function writes the output
function writeGen(maxNums)
{
 for(var i=0; i<maxNums; i++)
 {
  document.write('<font face="verdana,arial,helvetica" size="-1" color="#3366CC"><b>' + pickArr1[i', ' + pickArr2[i', ' + pickArr3[i'</b></font><br>');
 }
}

/*
  Modify the above function to suit your
  output needs.
*/

new writeGen(5);
</script>
<!--END BODY SECTION CODE-->
</td></tr></table>
<br>Reload the page to see a set of another unique random sets. (The page may take time to load)
<br><br>

<hr color="#808080">
&#1692002 <a href="http://www.qiksearch.com" title="Click here to visit Qiksearch.com"><font color="#3366CC">Premshree Pillai</font></a>.
</font>
</td></tr></table>
</body>
</html>

           
       



-

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

JAVASCRIPT DHTML TUTORIALS

 Navioo Language Basics
» Random