Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Unique Random Numbers II

    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.

    Code:
    <!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>
    Last edited by premshree; 08-22-2002 at 01:42 PM.

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts

    optimized functions

    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.

    Code:
    <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>

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'll try that out.

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

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    The following function will return pick numbers out of the numbers from 1 to tot with all numbers being unique.

    Code:
    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);
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •