PDA

View Full Version : Unique Random Numbers II


premshree
08-22-2002, 02:38 PM
This script is a slightly modified version of "Unique Random Numbers". In this script it becomes easier to implement more than one instances of "Picking 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.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Unique Random Numbers II</title>
<!--BEGIN HEAD SECTION CODE-->
<script language="JavaScript">
// Unique Random Numbers II
// -Picks a number of unique random numbers from an array
// By Premshree Pillai
// http://www.qiksearch.com, http://javascript.qik.cjb.net

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];
document.write('<b>' + numArr[currNum] + '</b> <font color="#808080">|</font> ');
/* Modify above line for a different format output */
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>
<!--END HEAD SECTION CODE-->
<body bgcolor="#FFFFFF">

<!--BEGIN BODY SECTION CODE-->
<script language="JavaScript">
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); // Call the function, the argument is the number of elements you want to pick.
// Here we pick 5 unique random numbers
</script>
<!--END BODY SECTION CODE-->

</body>
</html>


:thumbsup:

kansel
08-22-2002, 06:36 PM
I built a lotto picker a few weeks ago and went through the same iteration hassle. While it does eventually produce a unique pick of numbers, it's not quite optimal. If pushed hard - trying to pick 10 numbers out of 11 - it could conceivably run through 30 or more iterations before it filled all 10 slots.

I remembered back in my school days I had a poker program written in C++. The dealer class had a method to generate random cards. I adapted that method to a javascript function and here it is (sans cards).

The first function will return pickNum numbers in an array from a range starting with start and ending with end (inclusive).

The second function will return pickNum numbers in an array from a user-specified pool array, much like your script.

The script only makes one pass through the pool to assign the unique random numbers. It does this by replacing the random number with the last number in the array and reducing the available numbers by one.


<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function sortNumeric(a,b){return a-b};

function pickUniqueConsecutive(numPick,start,end){
var size = end - start + 1;
var unique = new Array(numPick);
var choose, temp;
if(size < 1) size = start - end + 1;
if(numPick > size) return alert("you can't pick " + numPick + " unique numbers from " + size + " numbers!");
var pool = new Array(size);
for(var i = 0; i < pool.length; i++)
pool[i] = i + start;
for(var i = 0; i < unique.length; i++){
choose = Math.floor(Math.random() * size);
unique[i] = pool[choose];
size--;
temp = pool[choose];
pool[choose] = pool[size];
pool[size] = temp;
}
return unique.sort(sortNumeric);
}

function pickUniqueArray(numPick,pool){
var size = pool.length;
var unique = new Array(numPick);
var choose, temp;
for(var i = 0; i < unique.length; i++){
choose = Math.floor(Math.random() * size);
unique[i] = pool[choose];
size--;
temp = pool[choose];
pool[choose] = pool[size];
pool[size] = temp;
}
return unique.sort(sortNumeric);
}
alert(pickUniqueConsecutive(3,1,10));
alert(pickUniqueArray(3,[2,3,5,7,11,13,17,19,23,29]));

//-->
</SCRIPT>

kansel
08-22-2002, 06:41 PM
guess I should be a good little programmer and supply variable definitions.

pickNum = integer, how many unique random numbers do you want to pick?
start = integer, first number in a range of consecutive numbers
end = integer, last number in a range of consecutive numbers
pool = array, user defined array of 'unique' numbers

both functions will return an array of size pickNum filled with unique random numbers.

premshree
08-22-2002, 08:02 PM
I'll try that out.

Here's a link to a LottoPicker : (looks good with imges!)
http://www.ourindooroopilly.com/lottopicker.html

felgall
05-02-2006, 05:34 AM
The following function will return pick numbers out of the numbers from 1 to tot with all numbers being unique.

function picks(pick,tot) {
for (var i = 0; i < tot; i++) ary[i] = i+1;
function randOrd(){return (Math.round(Math.random())-0.5); }
ary.sort(randOrd);
return ary.slice(0,pick);
}