How to create a unique random number.

 

By Michael Smith

14th May 2014


 

Sometimes you need to create a series of unique random numbers. An example of which is a lottery number generator program. Or a bingo game. Or a routine to select a different random scenery item  to use in a parallax scrolling background, avoiding bunches of same scenery items. Or to select a random boy and a random girl to join other sets of random boys girls in a battle to the death.

With many programming problems I say just ask: “How would you do it in real life?”

Say you wanted to choose five random numbers from 1 to 10.  And you don’t want a number that has been picked having an opportunity to be picked again.  You could just write each number on a slip of paper, fold the paper up and drop into a bowl.  Someone could then reach into the bowl and select a five slips.

So just do the same with your program.  Make a ten element array (the bowl). Each element holds one of the ten numbers (the folded up pieces of paper) Pick a random element and shrink the array to nine elements, removing the number just chosen from the pool (draw a piece of paper from the bowl.) Rinse and repeat.

I bet what you’d really like to see now is a flow chart?

Unique random numbers

What a lovely picture, but how about an example?

Oh OK then, here you go, a snippet to generate 5 unique random numbers between 1 and 10 inclusive (javascript & HTML5):

<!DOCTYPE HTML>
<html>
   <body>
      <button type="button" onclick="getRandomNos()">Generate</button>
      <p id="nos"></p>
      <script>
         function getRandomNos() {
            r="";
            bowl=['1','2','3','4','5','6','7','8','9','10'];
            for(i=0;i<5;i++)
            {
               rand=Math.floor(Math.random() * bowl.length);
               r+=bowl[rand]+",";
               temp=new Array();
               temp.length=bowl.length-1;
               ti=0;
               for (j=0;j<bowl.length;j++){
                  if (j!=rand){
                     temp[ti]=bowl[j];
                     ti++;
                  }
               }
               bowl.length=temp.length;
               bowl=temp;
            }
            r=r.slice(0,-1);// take the last "," off
            document.getElementById("nos").innerHTML = r;
         }
      </script>
   </body>
</html>

So just copy and paste the above into a plain text editor like notepad, save it as a html page and open it in your browser.


What’s that you say? “Can’t I just em-bed the code in this page for you?” Go on then y’ cheeky little scamp: 

Press button for unique random number magic

And there you go, hope you found it useful. If you’d like an e-mail whenever I post my narcissistic ramblings interesting articles, then please subscribe to my blog (top of page, right hand panel).

“To part is the lot of all mankind. The world is a scene of constant leave-taking, and the hands that grasp in cordial greeting today, are doomed ere long to unite for the the last time, when the quivering lips pronounce the word – ‘Farewell” - R.M. Ballantyne