JavaScript DHTML/Language Basics/Random — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 09:59, 26 мая 2010
Содержание
A Random Ad Display Page
<HTML>
<HEAD>
<TITLE>Displaying Random Ads</TITLE>
<SCRIPT LANGUAGE="JavaScript"><!--
urls = new Array("http://www.wbex.ru",
"http://www.google.ru",
"http://www.wbex.ru")
function insertAd() {
adIX = Math.round(Math.random()*(urls.length-1));
document.write("<A HREF=""+urls[adIX]+"">");
document.writeln("</A>");
}
// --></SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript"><!--
insertAd();
// --></SCRIPT>
<H1>Displaying Random Ads</H1>
</body>
</HTML>
Find the random number in a range
<html>
<head>
<title>Random Quote</title>
<script type="text/javascript">
function getQuote() {
do {
iValue = Math.random();
document.write(iValue+"<BR>");
iValue *= 10;
document.write(iValue+"<BR>");
iValue = Math.floor(iValue);
document.write(iValue+"<BR>");
}
while (iValue > 4)
document.write(iValue);
}
getQuote();
</script>
</head>
<body>
</body>
</html>
Math Random number: a random number between 0 and 1
<html>
<body>
<script type="text/javascript">
document.write(Math.random())
</script>
</body>
</html>
Random link
<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>
Random number from 0 to 10: a random number from 0 to 10 using the random() and round()
<html>
<body>
<script type="text/javascript">
no=Math.random()*10
document.write(Math.round(no))
</script>
</body>
</html>
Random URL
<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>
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.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>
<!--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">
© 2002 <a href="http://www.qiksearch.ru" class="link">Premshree Pillai</a>.
</font>
</td></tr></table>
</body>
</html>
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.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>
<!--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.ru/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, 3 arrays) to 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.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("<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">
© 2002 <a href="http://www.qiksearch.ru" title="Click here to visit Qiksearch.ru"><font color="#3366CC">Premshree Pillai</font></a>.
</font>
</td></tr></table>
</body>
</html>