PDA

View Full Version : Random Number Generator



Jay Dog
07-07-2015, 08:49 PM
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.


<!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>

styxlawyer
07-08-2015, 08:13 AM
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:




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);
}

vwphillips
07-08-2015, 08:42 AM
<!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>

styxlawyer
07-08-2015, 09:13 PM
Apologies for the typo. That should of course be:




.
.
. 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);
}

styxlawyer
07-08-2015, 10:49 PM
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.



<!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>