JavaScript DHTML/Language Basics/Random

Материал из Web эксперт
Перейти к: навигация, поиск

A Random Ad Display Page

   <source lang="html4strict">
 

<HTML> <HEAD> <TITLE>Displaying Random Ads</TITLE> <SCRIPT LANGUAGE="JavaScript"></SCRIPT> </HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript"></SCRIPT>

Displaying Random Ads

</body> </HTML>


 </source>
   
  


Find the random number in a range

   <source lang="html4strict">
  

<html> <head> <title>Random Quote</title> <script type="text/javascript"> function getQuote() {

  do {
    iValue = Math.random(); 
    document.write(iValue+"
"); iValue *= 10; document.write(iValue+"
"); iValue = Math.floor(iValue); document.write(iValue+"
"); } while (iValue > 4) document.write(iValue);

} getQuote(); </script> </head> <body> </body> </html>


 </source>
   
  


Math Random number: a random number between 0 and 1

   <source lang="html4strict">
 

<html> <body> <script type="text/javascript">

   document.write(Math.random())

</script> </body> </html>



 </source>
   
  


Random link

   <source lang="html4strict">
 

<html> <body> <script type="text/javascript"> var r=Math.random() if (r>0.5){

   document.write("<a href="http://www.wbex.ru">wbex</a>")

}else{

   document.write("<a href="http://www.google.ca">Google!</a>")

} </script> </body> </html>



 </source>
   
  


Random number from 0 to 10: a random number from 0 to 10 using the random() and round()

   <source lang="html4strict">
 

<html> <body> <script type="text/javascript">

   no=Math.random()*10
   document.write(Math.round(no))
   

</script> </body> </html>



 </source>
   
  


Random URL

   <source lang="html4strict">
 

<HTML> <HEAD> <SCRIPT language="JavaScript"> var rand_url= new Array() rand_url[0]="http://www.wbex.ru"; rand_url[1]="http://www.google.ru"; rand_url[2]="http://www.msn.ru"; var rand_num= Math.floor(Math.random()*2); </SCRIPT> </HEAD> <BODY> <SCRIPT language="JavaScript">

  document.write("<A HREF=""+rand_url[rand_num]+"">"+rand_url[rand_num]+"</A>");

</SCRIPT> </BODY> </HTML>


 </source>
   
  


Unique Random Numbers

   <source lang="html4strict">
 

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

<script language="JavaScript"> // Unique Random Numbers // -Picks a number of unique random numbers from an array // (c) 2002 Premshree Pillai // http://www.qiksearch.ru // http://premshree.resource-locator.ru // E-mail : qiksearch@rediffmail.ru 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>

</head> <body bgcolor="#FFFFFF">

Unique Random Numbers


This JavaScript picks up a number of unique random elements from an array.

For example; if you have an array myArray consisting of 10 elements and want to pick 5 unique random elements. Suppose initially myArray[3] is picked randomly, then myArray[3] should not be picked again.

If you want to pick 4 numbers, call the function like this :
pickNums(4,myArray). This function will return an array consisting of 4 unique random numbers. Thus you can store this array like this :
var anotherArray=pickNums(4,myArray).
You can now use this array for displaying the elements in different formats.

Could be useful :-)

Here"s an example :

<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(""); for(var i=0; i<outArr.length; i++) {

 document.write("" + outArr[i] + " ");

} document.write(""); </script>



<a href="javascript:void(location.reload())" class="link">Reload</a> the page to see a set of another unique random numbers.


© 2002 <a href="http://www.qiksearch.ru" class="link">Premshree Pillai</a>.

</body> </html>


 </source>
   
  


Unique Random Sets

   <source lang="html4strict">
 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Unique Random Sets</title>

<script language="JavaScript"> // Unique Random Sets Picker // Based on : Unique Random Numbers // -http://www.qiksearch.ru/javascripts/random_numbers1.htm // -Picks a number of unique random numbers from an array // (c) 2002 Premshree Pillai // http://www.qiksearch.ru, http://javascript.qik.cjb.net // E-mail : qiksearch@rediffmail.ru 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>

</head> <body bgcolor="#FFFFFF">

Unique Random Sets


This JavaScript, based on <a href="http://www.qiksearch.ru/javascripts/urn20.htm">Unique Random Numbers II</a>, picks Unique Random Sets.

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, numArr1 to form the array pickArr1. Now each element of pickArr1 picks another element from the second array numArr2, which again is unique and random. Thus we form the array pickArr2. Similarly each element of pickArr2 picks unique random elements from numArr3 to form pickArr3.
i.e if the first element of pickArr1 picks the second element of numArr2, then no other element of pickArr1 should pick the second element of numArr2 again.

In effect what we are doing is picking equal number of unique random numbers from a number of arrays(In this exaple, 3 arrays) to form a number of unique random sets, containing number of elements same as the number of arrays.

Here"s an example :

<script language="JavaScript"> // (c) 2002 Premshree Pillai // http://www.qiksearch.ru, http://javascript.qik.cjb.net // E-mail : qiksearch@rediffmail.ru //---------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("" + pickArr1[i] + ", " + pickArr2[i] + ", " + pickArr3[i] + "
"); }

} /*

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

new writeGen(5); </script>


Reload the page to see a set of another unique random sets. (The page may take time to load)


© 2002 <a href="http://www.qiksearch.ru" title="Click here to visit Qiksearch.ru">Premshree Pillai</a>.

</body> </html>


 </source>