PDA

View Full Version : Unique Random Numbers


premshree
07-22-2002, 12:04 PM
This script picks a number of unique random numbers from an array.
Demo : http://www.qiksearch.com/javascripts/random_numbers1.htm


<script language="JavaScript">
// Unique Random Numbers Picker
// By Premshree Pillai

var numArr = new Array("0","1","2","3","4","5","6","7","8","9"); // Add elements here
var pickArr = new Array(); // The array that will be formed
var count=0;
var doFlag=false;
var iterations=0;

function pickNums(nums)
{
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];
document.write('<b>' + numArr[currNum] + '</b> <font color="#808080">|</font> ');
count+=1;
}
if(iterations<(numArr.length*3)) // Compare for max iterations you want
{
if((count<nums))
{
pickNums(nums);
}
}
else
{
location.reload();
}
}

pickNums(5); // Call the function, the argument is the number of elements you want to pick.
// Here we pick 5 unique random numbers
</script>


:thumbsup:

ScriptsKeeper
07-24-2002, 09:03 PM
Can the above script be used for midi or graphics? If so, what are the modifications? Thanks!

premshree
08-22-2002, 01:31 PM
Yes, you can use the script for midi/graphics. You will have to make modifications, in the ouput format.

See this link :
http://www.ourindooroopilly.com/lottopicker.html

:thumbsup: