Javascript random numbers without dupes

  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post August 17th, 2011, 1:42 am

Hi Poly

I know I am probably getting very picky now, but I hope I can ask for one last thing.

When numbers get generated they look like:

2 4 6 8 10 12

To make them clearer I would like them to look like:

2 : 4 : 6 : 8 : 10 : 12

I tried adding this to your expression:

document.lottery.numbers.value = document.lottery.numbers.value + loop + " : ";

but then I get:

2 : 4 : 6 : 8 : 10 : 12 :

I get the separator at the end also which is what I do not want.

Could you also impement separator but it is not to be put after last digit

Thanks
http://www.schembrionics.com
The Ultimate Solutions Center
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 17th, 2011, 1:42 am

  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post August 17th, 2011, 4:19 am

Hi Poly

external .js file would be a problem for me.

please try to do everything inside script

Thanks
http://www.schembrionics.com
The Ultimate Solutions Center
  • Poly
  • Guru
  • Guru
  • User avatar
  • Joined: Jul 31, 2004
  • Posts: 1054
  • Loc: Same place you left me.
  • Status: Offline

Post August 17th, 2011, 3:19 pm

Hi wpas,

I have made the updates. It now validates that the input boxes are not empty, then runs the generate script, then trims the trailing characters leaving you with "1 : 2 : 3 : 4 : 5 : 6". I have broken it into 3 scripts and have commented what each script does in the code.

Updated Code:


Code: [ Select ]
<!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>Random Lotto Number Generator</title>
</head>

<body>

<!-- Generate Random numbers, check to make sure we have no duplicates, order them in an ascending order, output to text box -->
<script language="JavaScript">
var loop;
var randy;

function make_array(size){
this.length = size;
      for (a=0; a<size; a++) this[a]="";
}

function generatenos(){
hold = new make_array(document.lottery.max_number.value);
document.lottery.numbers.value = "";
    for(loop=0;loop<document.lottery.balls_drawn.value;loop++){
        randy = Math.round(Math.random()*(document.lottery.max_number.value-1));
        randy++;
        if(hold[randy]==1){
            loop--;
            continue;
            }
        if(hold[randy]!=1){
            hold[randy]=1;
            }
            }
    for(loop=0;loop<=document.lottery.max_number.value;loop++){
        if(hold[loop]==1)
            document.lottery.numbers.value = document.lottery.numbers.value + loop + " : ";
    }
}
</script>

<!-- Fix for NS Browsers -->
<script language="JavaScript">
var axel = Math.random() + "";
var PageId = axel * 1000000000000000000;
NS4 = document.layers;
if (NS4) {
origWidth = innerWidth;
origHeight = innerHeight;
}
function reDo() {
if (innerWidth != origWidth || innerHeight != origHeight)
location.reload();
}
if (NS4) onresize = reDo;
</script>

<!-- Validate our data, if box text boxes are filled in, use the generatenos function -->
<script type="text/javascript">
function validate(balls_drawn,max_number){
    if (balls_drawn.value=="")
    {
        alert("Please fill in the number of balls.");
    }
    else if (max_number.value=="")
    {
        alert("Please fill in the max number.");
    }
    else
    {
        generatenos();
    }
}

</script>

<!-- Going to trim the last three characters in the text box when you press the Generate Numbers button. Last 3 characters will always be SPACE:SPACE. -->
<script type="text/javascript">
function removeLastColon(myStr) {
var myStr = myStr.value
var strLen = myStr.length;
myStr = myStr.slice(0,strLen-3);
document.lottery.numbers.value = myStr;
}

</script>

<center>
<form name="lottery">Total number of balls:
   <input type="text" name="balls_drawn" size="3" value="6">
   Maximum Number:
   <input type="text" name="max_number" size="3" value="49">
   <input type="button" value="Generate Numbers" onclick="validate(balls_drawn, max_number); removeLastColon(numbers)" name="button2"><br />
   <input type="text" name="numbers" size="29" value="Good luck!" />
   <input type="reset">
   </form>
</tr>
</tbody></table></center>
</body>
</html>
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Random Lotto Number Generator</title>
  6. </head>
  7. <body>
  8. <!-- Generate Random numbers, check to make sure we have no duplicates, order them in an ascending order, output to text box -->
  9. <script language="JavaScript">
  10. var loop;
  11. var randy;
  12. function make_array(size){
  13. this.length = size;
  14.       for (a=0; a<size; a++) this[a]="";
  15. }
  16. function generatenos(){
  17. hold = new make_array(document.lottery.max_number.value);
  18. document.lottery.numbers.value = "";
  19.     for(loop=0;loop<document.lottery.balls_drawn.value;loop++){
  20.         randy = Math.round(Math.random()*(document.lottery.max_number.value-1));
  21.         randy++;
  22.         if(hold[randy]==1){
  23.             loop--;
  24.             continue;
  25.             }
  26.         if(hold[randy]!=1){
  27.             hold[randy]=1;
  28.             }
  29.             }
  30.     for(loop=0;loop<=document.lottery.max_number.value;loop++){
  31.         if(hold[loop]==1)
  32.             document.lottery.numbers.value = document.lottery.numbers.value + loop + " : ";
  33.     }
  34. }
  35. </script>
  36. <!-- Fix for NS Browsers -->
  37. <script language="JavaScript">
  38. var axel = Math.random() + "";
  39. var PageId = axel * 1000000000000000000;
  40. NS4 = document.layers;
  41. if (NS4) {
  42. origWidth = innerWidth;
  43. origHeight = innerHeight;
  44. }
  45. function reDo() {
  46. if (innerWidth != origWidth || innerHeight != origHeight)
  47. location.reload();
  48. }
  49. if (NS4) onresize = reDo;
  50. </script>
  51. <!-- Validate our data, if box text boxes are filled in, use the generatenos function -->
  52. <script type="text/javascript">
  53. function validate(balls_drawn,max_number){
  54.     if (balls_drawn.value=="")
  55.     {
  56.         alert("Please fill in the number of balls.");
  57.     }
  58.     else if (max_number.value=="")
  59.     {
  60.         alert("Please fill in the max number.");
  61.     }
  62.     else
  63.     {
  64.         generatenos();
  65.     }
  66. }
  67. </script>
  68. <!-- Going to trim the last three characters in the text box when you press the Generate Numbers button. Last 3 characters will always be SPACE:SPACE. -->
  69. <script type="text/javascript">
  70. function removeLastColon(myStr) {
  71. var myStr = myStr.value
  72. var strLen = myStr.length;
  73. myStr = myStr.slice(0,strLen-3);
  74. document.lottery.numbers.value = myStr;
  75. }
  76. </script>
  77. <center>
  78. <form name="lottery">Total number of balls:
  79.    <input type="text" name="balls_drawn" size="3" value="6">
  80.    Maximum Number:
  81.    <input type="text" name="max_number" size="3" value="49">
  82.    <input type="button" value="Generate Numbers" onclick="validate(balls_drawn, max_number); removeLastColon(numbers)" name="button2"><br />
  83.    <input type="text" name="numbers" size="29" value="Good luck!" />
  84.    <input type="reset">
  85.    </form>
  86. </tr>
  87. </tbody></table></center>
  88. </body>
  89. </html>
Every job is a self-portrait of the person who did it: Autograph your work with excellence.
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post August 17th, 2011, 6:29 pm

Hi Poly

Works Great!

You the man

Thanks again for all your help

Joe
http://www.schembrionics.com
The Ultimate Solutions Center
  • Poly
  • Guru
  • Guru
  • User avatar
  • Joined: Jul 31, 2004
  • Posts: 1054
  • Loc: Same place you left me.
  • Status: Offline

Post August 17th, 2011, 6:47 pm

Not a problem.
Every job is a self-portrait of the person who did it: Autograph your work with excellence.

Post Information

  • Total Posts in this topic: 20 posts
  • Users browsing this forum: No registered users and 122 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.