Results 1 to 5 of 5

Thread: Random Number Generator

  1. #1
    Join Date
    Oct 2012
    Location
    England
    Posts
    103
    Thanks
    28
    Thanked 2 Times in 1 Post

    Default Random Number Generator

    Hi guys,

    I've just modified this sample bit of code I just found on the net but how can I make it so that the same number doesn't come up more than once?

    Thanks.

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title><SCRIPT LANGUAGE="JavaScript" TYPE="text/JavaScript">
    <!--
    // Get named HTML object
    function GetObject(name)
    {
    	var o=null;
    	if(document.getElementById)
    		o=document.getElementById(name);
    	else if(document.all)
    		o=document.all.item(name);
    	else if(document.layers)
    		o=document.layers[name];
    	return o;
    }
    function getrnum()
    {
    	var rf=parseInt(GetObject("rfrom").value,10);
    	var rt=parseInt(GetObject("rto").value,10);
    	if (isNaN(rf)) rf=0;
    	if (isNaN(rt)) rt=0;
    	if (rt<rf)
    		{
    			var t=rt;
    			rt=rf;
    			rf=t;
    		}
    	var r=Math.floor(rf+Math.random()*(rt-rf+1));
    	GetObject("rfrom").value=String(rf);
    	GetObject("rto").value=String(rt);
    	GetObject("rnum").innerHTML=String(r);
    }
    -->
    </SCRIPT>
    
    </head>
    
    <body>
    
    	<table width="357" border="0" cellpadding="0" cellspacing="0">
    	  <tr>
    	    <td colspan="3" align="center" valign="middle"><H1>Random Numbers</H1></td>
          </tr>
    	  <tr>
    	    <td width="147" align="center" valign="middle">between
    </td>
    	    <td width="87" rowspan="2" align="center" valign="middle"><B><SPAN id="rnum" style="font-size:300%">59</SPAN></B></td>
    	    <td width="109" rowspan="2" align="center" valign="middle"><BUTTON onclick="javascript:getrnum()">Get number</BUTTON></td>
          </tr>
    	  <tr>
    	    <td align="center" valign="middle"><INPUT type="text" id="rfrom" size="3" value="1">
    	      and
      <INPUT type="text" id="rto" size="3" value="100"></td>
          </tr>
      </table>
    
    </body>
    </html>

  2. #2
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    Just keep an array of the numbers which have already appeared and don't exit the function "getrnum()" until you find one that hasn't been used already. However, the numbers will no longer be random and it may take a long time to find an unused number when you have called the function a few times. You will also have to allow for the situation where all 100 numbers have been used and "getrnum()" will hang.

    Try this for a start:

    Code:
    var numbersUsed = new Array();
    
    function getrnum()
    {
    	var rf=parseInt(GetObject("rfrom").value,10);
    	var rt=parseInt(GetObject("rto").value,10);
    	if (isNaN(rf)) rf=0;
    	if (isNaN(rt)) rt=0;
    	if (rt<rf)
    		{
    			var t=rt;
    			rt=rf;
    			rf=t;
    		}
    	var r=Math.floor(rf+Math.random()*(rt-rf+1));
            do {
              if (numbersUsed[r] == 1) {
                r=Math.floor(rf+Math.random()*(rt-rf+1));
              }
            } while (numbersUsed[r] != 1);
            numbersUsed[r] = 1;
    	GetObject("rfrom").value=String(rf);
    	GetObject("rto").value=String(rt);
    	GetObject("rnum").innerHTML=String(r);
    }

  3. #3
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <SCRIPT TYPE="text/JavaScript">
    <!--
    // Get named HTML object
    function GetObject(name)
    {
    	return document.getElementById(name);
    }
    function getrnum()
    {
    	var rf=parseInt(GetObject("rfrom").value,10);
    	var rt=parseInt(GetObject("rto").value,10);
        var cr=GetObject("rnum").innerHTML*1;
    	if (isNaN(rf)||isNaN(rt)||rf<1||rt<1||rf==rt){
         return;
    	}
    	if (rt<rf)
    		{
    			var t=rt;
    			rt=rf;
    			rf=t;
    		}
    	var r=Math.floor(rf+Math.random()*(rt-rf+1));
        while (cr==r){
    	 r=Math.floor(rf+Math.random()*(rt-rf+1));
        }
    	GetObject("rfrom").value=String(rf);
    	GetObject("rto").value=String(rt);
    	GetObject("rnum").innerHTML=String(r);
    }
    -->
    </SCRIPT>
    
    </head>
    
    <body>
    
    	<table width="357" border="0" cellpadding="0" cellspacing="0">
    	  <tr>
    	    <td colspan="3" align="center" valign="middle"><H1>Random Numbers</H1></td>
          </tr>
    	  <tr>
    	    <td width="147" align="center" valign="middle">between
    </td>
    	    <td width="87" rowspan="2" align="center" valign="middle"><B><SPAN id="rnum" style="font-size:300%">59</SPAN></B></td>
    	    <td width="109" rowspan="2" align="center" valign="middle"><BUTTON onclick="javascript:getrnum()">Get number</BUTTON></td>
          </tr>
    	  <tr>
    	    <td align="center" valign="middle"><INPUT type="text" id="rfrom" size="3" value="1">
    	      and
      <INPUT type="text" id="rto" size="3" value="100"></td>
          </tr>
      </table>
    
    </body>
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  4. #4
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    Apologies for the typo. That should of course be:

    Code:
    .
    .
    .        do {
              if (numbersUsed[r] == 1) {
                r=Math.floor(rf+Math.random()*(rt-rf+1));
              }
            } while (numbersUsed[r] == 1);
            numbersUsed[r] = 1;
    	GetObject("rfrom").value=String(rf);
    	GetObject("rto").value=String(rt);
    	GetObject("rnum").innerHTML=String(r);
    }

  5. #5
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    OK, here's a version which will show each number in the range only once and won't hang when you have used all the possible numbers. Changing either of the start or end numbers will allow the function to run again. JavaScript arrays cannot use negative numbers as indices (they can be used as properties) so these have been disallowed in the initialisation.

    Hope it helps.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <SCRIPT TYPE="text/JavaScript">
    <!--
    // Get named HTML object
    function GetObject(name)
    {
    	return document.getElementById(name);
    }
    
    var numbersUsed = new Array();
    var range = -1, oldRt = -1, oldRf = -1, somethingChanged = 0, loops = 0;
    
    function getrnum()
    {
    	var rf=parseInt(GetObject("rfrom").value,10);
    	var rt=parseInt(GetObject("rto").value,10);
    	if (isNaN(rf) || (rf < 0)) rf=0;
    	if (isNaN(rt) || (rt < 0)) rt=0;
    	if (rt<rf)
    		{
    			var t=rt;
    			rt=rf;
    			rf=t;
    		}
    	if (oldRt == -1)
    	{
    		oldRt = rt;
    	}
    	if (oldRf == -1)
    	{
    		oldRf = rf;
    	}
    	if (range == -1)
    	{
    		range = (rt-rf)+1;
    	}
    	if (oldRt != rt)
    	{
    		oldRt = rt;
    		somethingChanged = 1;
    	}
    	if (oldRf != rf)
    	{
    		oldRf = rf;
    		somethingChanged = 1;
    	}
    	if (somethingChanged != 0) /* Now check if the something has changed. */
    	{
        range = (rt-rf)+1;
    		numbersUsed.length = 0; /* Empty the numbersUsed array. */
    		loops = 0;
    		GetObject("loops").innerHTML="";
    		somethingChanged = 0;
    	}
    	if (loops == range)
    	{
    		GetObject("loops").innerHTML="All possible numbers used.";
    		return;
    	}
    	var r=Math.floor(rf+Math.random()*(rt-rf+1));
      do {
        if (numbersUsed[r] == 1) {
          r=Math.floor(rf+Math.random()*(rt-rf+1));
        }
      } while (numbersUsed[r] == 1);
      numbersUsed[r] = 1;
      loops++;
      GetObject("rfrom").value=String(rf);
      GetObject("rto").value=String(rt);
      GetObject("rnum").innerHTML=String(r);
    }
    -->
    </SCRIPT>
    
    </head>
    
    <body>
    
    	<table width="357" border="0" cellpadding="0" cellspacing="0">
    	  <tr>
    	    <td colspan="3" align="center" valign="middle"><H1>Random Numbers</H1></td>
          </tr>
    	  <tr>
    	    <td width="147" align="center" valign="middle">between
    </td>
    	    <td width="87" rowspan="2" align="center" valign="middle"><B><SPAN id="rnum" style="font-size:300%">--</SPAN></B></td>
    	    <td width="109" rowspan="2" align="center" valign="middle"><BUTTON onclick="javascript:getrnum()">Get number</BUTTON></td>
          </tr>
    	  <tr>
    	    <td align="center" valign="middle"><INPUT type="text" id="rfrom" size="3" value="1">
    	      and
      <INPUT type="text" id="rto" size="3" value="100"></td>
          </tr>
      </table>
      <p id="loops"></p>
    </body>
    </html>

Similar Threads

  1. number generator
    By james438 in forum PHP
    Replies: 11
    Last Post: 04-27-2012, 06:53 AM
  2. [help] Javascript Random Generator
    By mamamiaw in forum JavaScript
    Replies: 5
    Last Post: 08-06-2008, 03:31 AM
  3. Random name generator?
    By mevetts in forum JavaScript
    Replies: 17
    Last Post: 06-24-2007, 07:20 PM
  4. Random Picture Generator
    By thehealey in forum Other
    Replies: 1
    Last Post: 06-09-2007, 03:32 AM
  5. Random Game Generator
    By MommaCassie in forum JavaScript
    Replies: 3
    Last Post: 01-24-2006, 07:38 PM

Bookmarks

Posting Permissions

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