Javascript random numbers without dupes

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi All

I am just learning about javascript and would appreciate some help.

I have the following code:

Code: [ Select ]
<script>
function mylotto() {
var rlts = document.getElementsByName("result");
for(var i=0 ; i<=5; i++) {
rlts[i].value=""; // clear results
}
var max=eval("document.lot.maxnum.value");
if (max.length<1) {
alert ("You must choose a number for the maximum value!");
document.lot.maxnum.focus();
return false;
}
for(var i=0 ; i<=5; i++) {
rlts[i].value=(Math.floor(Math.random()*max)+1);
}
}
</script>
<form name="lot">
Enter the Maximum Number Value<input type="text" name="maxnum" value="49" size="2" STYLE="font-size:15pt"><br><br>
<input type="button" STYLE="font-size:12pt" value="Generate Lotto 6/49 Numbers" onclick="mylotto()">
<br><br>
<input type="text" size="2" STYLE="font-size:15pt" name="result">

<input type="text" STYLE="font-size:15pt" size="2" name="result">

<input type="text" size="2" STYLE="font-size:15pt"  name="result">

<input type="text" size="2" STYLE="font-size:15pt"  name="result">

<input type="text" size="2" STYLE="font-size:15pt"  name="result">

<input type="text" size="2" STYLE="font-size:15pt"  name="result">

</form>
  1. <script>
  2. function mylotto() {
  3. var rlts = document.getElementsByName("result");
  4. for(var i=0 ; i<=5; i++) {
  5. rlts[i].value=""; // clear results
  6. }
  7. var max=eval("document.lot.maxnum.value");
  8. if (max.length<1) {
  9. alert ("You must choose a number for the maximum value!");
  10. document.lot.maxnum.focus();
  11. return false;
  12. }
  13. for(var i=0 ; i<=5; i++) {
  14. rlts[i].value=(Math.floor(Math.random()*max)+1);
  15. }
  16. }
  17. </script>
  18. <form name="lot">
  19. Enter the Maximum Number Value<input type="text" name="maxnum" value="49" size="2" STYLE="font-size:15pt"><br><br>
  20. <input type="button" STYLE="font-size:12pt" value="Generate Lotto 6/49 Numbers" onclick="mylotto()">
  21. <br><br>
  22. <input type="text" size="2" STYLE="font-size:15pt" name="result">
  23. <input type="text" STYLE="font-size:15pt" size="2" name="result">
  24. <input type="text" size="2" STYLE="font-size:15pt"  name="result">
  25. <input type="text" size="2" STYLE="font-size:15pt"  name="result">
  26. <input type="text" size="2" STYLE="font-size:15pt"  name="result">
  27. <input type="text" size="2" STYLE="font-size:15pt"  name="result">
  28. </form>


It is basically a six number random generator.

It works good untill you run it a few times and start to get duplicate numbers.

I have hunted around the web and it seems I should "shuffle" the numbers.
I have tried to incorporate but bombs out all the time.

Can someone show me how to do this and where to put the code.

If there is another way to prevent duplicates, I will gladly listen, but just remember I a a newbie

Thanks

Joe
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

Hello wpas,

The following code will randomly generate a total of 6 numbers plus 1 bonus number. The maximum number that can be generated is 49. It will then ouput the data to a table.

The Script:


Code: [ Select ]
<script type="text/javascript" language="javascript">
    var lotto = {
        max : 49,
        num : 6,
        stars: 1
    }
            
    function lottery() {
        printNumbers(getNumbers((lotto.num+lotto.stars),lotto.max),"lotto");
    }

    function printNumbers(numbers,type){
        for(var x in numbers){
            document.getElementById(type+x).innerHTML = numbers[x];
        }
    }

    function getNumbers(totalBalls,balls) {
        var numbers = [];
        for (var i = balls; i > 0; i--){
            numbers.push(i);
        }
        numbers.sort(
            function(){
                return (Math.round(Math.random())-0.5);
            }
        );
        return numbers.slice(0,totalBalls);
    }
</script>
  1. <script type="text/javascript" language="javascript">
  2.     var lotto = {
  3.         max : 49,
  4.         num : 6,
  5.         stars: 1
  6.     }
  7.             
  8.     function lottery() {
  9.         printNumbers(getNumbers((lotto.num+lotto.stars),lotto.max),"lotto");
  10.     }
  11.     function printNumbers(numbers,type){
  12.         for(var x in numbers){
  13.             document.getElementById(type+x).innerHTML = numbers[x];
  14.         }
  15.     }
  16.     function getNumbers(totalBalls,balls) {
  17.         var numbers = [];
  18.         for (var i = balls; i > 0; i--){
  19.             numbers.push(i);
  20.         }
  21.         numbers.sort(
  22.             function(){
  23.                 return (Math.round(Math.random())-0.5);
  24.             }
  25.         );
  26.         return numbers.slice(0,totalBalls);
  27.     }
  28. </script>

This is the script that will generate the numbers and prepare them to be output into an HTML table. This code needs to be placed inside the <head> tag of your document.

Displaying The Numbers:


Code: [ Select ]
<table width="700" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td width="100" align="center">Lotto #1:</td>
  <td width="100" align="center">Lotto #2:</td>
  <td width="100" align="center">Lotto #3:</td>
  <td width="100" align="center">Lotto #4:</td>
  <td width="100" align="center">Lotto #5:</td>
  <td width="100" align="center">Lotto #6:</td>
  <td width="100" align="center">Bonus:</td>
 </tr></table><h3>
 <table width="700" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td width="100" align="center" id="lotto0">&nbsp;</td>
  <td width="100" align="center" id="lotto1">&nbsp;</td>
  <td width="100" align="center" id="lotto2">&nbsp;</td>
  <td width="100" align="center" id="lotto3">&nbsp;</td>
  <td width="100" align="center" id="lotto4">&nbsp;</td>
  <td width="100" align="center" id="lotto5">&nbsp;</td>
  <td width="100" align="center" id="lotto6">&nbsp;</td>
 </tr>
</table><br /></h3>
<input type="button" value=" Generate " id="reload" onclick="lottery()">
  1. <table width="700" border="0" cellspacing="0" cellpadding="0">
  2.  <tr>
  3.   <td width="100" align="center">Lotto #1:</td>
  4.   <td width="100" align="center">Lotto #2:</td>
  5.   <td width="100" align="center">Lotto #3:</td>
  6.   <td width="100" align="center">Lotto #4:</td>
  7.   <td width="100" align="center">Lotto #5:</td>
  8.   <td width="100" align="center">Lotto #6:</td>
  9.   <td width="100" align="center">Bonus:</td>
  10.  </tr></table><h3>
  11.  <table width="700" border="0" cellspacing="0" cellpadding="0">
  12.  <tr>
  13.   <td width="100" align="center" id="lotto0">&nbsp;</td>
  14.   <td width="100" align="center" id="lotto1">&nbsp;</td>
  15.   <td width="100" align="center" id="lotto2">&nbsp;</td>
  16.   <td width="100" align="center" id="lotto3">&nbsp;</td>
  17.   <td width="100" align="center" id="lotto4">&nbsp;</td>
  18.   <td width="100" align="center" id="lotto5">&nbsp;</td>
  19.   <td width="100" align="center" id="lotto6">&nbsp;</td>
  20.  </tr>
  21. </table><br /></h3>
  22. <input type="button" value=" Generate " id="reload" onclick="lottery()">

You will notice this contains 3 elements. First is the table containing the labels. Second is another table, with the output data. Third is a button that will generate the numbers each time you press it, calling the lottery function from our script above. You can apply styles or any other effects to these tables and button.

Complete 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>
<script type="text/javascript" language="javascript">
    var lotto = {
        max : 49,
        num : 6,
        stars: 1
    }
            
    function lottery() {
        printNumbers(getNumbers((lotto.num+lotto.stars),lotto.max),"lotto");
    }

    function printNumbers(numbers,type){
        for(var x in numbers){
            document.getElementById(type+x).innerHTML = numbers[x];
        }
    }

    function getNumbers(totalBalls,balls) {
        var numbers = [];
        for (var i = balls; i > 0; i--){
            numbers.push(i);
        }
        numbers.sort(
            function(){
                return (Math.round(Math.random())-0.5);
            }
        );
        return numbers.slice(0,totalBalls);
    }
</script>
</head>

<body>

<table width="700" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td width="100" align="center">Lotto #1:</td>
  <td width="100" align="center">Lotto #2:</td>
  <td width="100" align="center">Lotto #3:</td>
  <td width="100" align="center">Lotto #4:</td>
  <td width="100" align="center">Lotto #5:</td>
  <td width="100" align="center">Lotto #6:</td>
  <td width="100" align="center">Bonus:</td>
 </tr></table><h3>
 <table width="700" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td width="100" align="center" id="lotto0">&nbsp;</td>
  <td width="100" align="center" id="lotto1">&nbsp;</td>
  <td width="100" align="center" id="lotto2">&nbsp;</td>
  <td width="100" align="center" id="lotto3">&nbsp;</td>
  <td width="100" align="center" id="lotto4">&nbsp;</td>
  <td width="100" align="center" id="lotto5">&nbsp;</td>
  <td width="100" align="center" id="lotto6">&nbsp;</td>
 </tr>
</table><br /></h3>
<input type="button" value=" Generate " id="reload" onclick="lottery()">
</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. <script type="text/javascript" language="javascript">
  7.     var lotto = {
  8.         max : 49,
  9.         num : 6,
  10.         stars: 1
  11.     }
  12.             
  13.     function lottery() {
  14.         printNumbers(getNumbers((lotto.num+lotto.stars),lotto.max),"lotto");
  15.     }
  16.     function printNumbers(numbers,type){
  17.         for(var x in numbers){
  18.             document.getElementById(type+x).innerHTML = numbers[x];
  19.         }
  20.     }
  21.     function getNumbers(totalBalls,balls) {
  22.         var numbers = [];
  23.         for (var i = balls; i > 0; i--){
  24.             numbers.push(i);
  25.         }
  26.         numbers.sort(
  27.             function(){
  28.                 return (Math.round(Math.random())-0.5);
  29.             }
  30.         );
  31.         return numbers.slice(0,totalBalls);
  32.     }
  33. </script>
  34. </head>
  35. <body>
  36. <table width="700" border="0" cellspacing="0" cellpadding="0">
  37.  <tr>
  38.   <td width="100" align="center">Lotto #1:</td>
  39.   <td width="100" align="center">Lotto #2:</td>
  40.   <td width="100" align="center">Lotto #3:</td>
  41.   <td width="100" align="center">Lotto #4:</td>
  42.   <td width="100" align="center">Lotto #5:</td>
  43.   <td width="100" align="center">Lotto #6:</td>
  44.   <td width="100" align="center">Bonus:</td>
  45.  </tr></table><h3>
  46.  <table width="700" border="0" cellspacing="0" cellpadding="0">
  47.  <tr>
  48.   <td width="100" align="center" id="lotto0">&nbsp;</td>
  49.   <td width="100" align="center" id="lotto1">&nbsp;</td>
  50.   <td width="100" align="center" id="lotto2">&nbsp;</td>
  51.   <td width="100" align="center" id="lotto3">&nbsp;</td>
  52.   <td width="100" align="center" id="lotto4">&nbsp;</td>
  53.   <td width="100" align="center" id="lotto5">&nbsp;</td>
  54.   <td width="100" align="center" id="lotto6">&nbsp;</td>
  55.  </tr>
  56. </table><br /></h3>
  57. <input type="button" value=" Generate " id="reload" onclick="lottery()">
  58. </body>
  59. </html>

The above contains all of the code for a complete HTML/JavaScript example on how to generate random lottery numbers without any repeated numbers.

Post up if you have any difficulties with the code.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Poly

Worked pretty good

Just need one more thing if you don't mind.

Is it possible to sort the numbers displayed so that if we get

23 7 31 4 45 43

We actually will see the sorted results

4 7 23 31 43 45

It makes it much easier to view

Thanks
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Poly

Your script requires head and body.

If you will notice my script only needed body

In the situation I am in, I can only insert javascript between<body> and </body>

Can you script be done for use in body section only

Thanks
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Again

I can use your script as it is.

The only problem is that it is not sorted

When I looked at your script, it did appear to have sorting but I do not see it in the result.

Is there an error or something

Thanks
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

I'll look into it as soon as I get a chance.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Poly

thanks for your response

could you possible also add a "Reset" button to clear the numbers.

I tried but cannot seem to make it work.

thanks

Joe
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

Sure, shouldn't be too hard. Soon as I'm back on my PC I'll take a look at it and see what I can figure out.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Thanks Poly

Look forward to your response

Joe
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

This has turned out to be a bit trickier than I expected. I have gotten quite a bit of it working how you are wanting, and am working on the reset button right now. Should have it working shortly.

Update: Finished!
This was pretty tricky, and I had to reference work done by Micky Penzance. Ultimately what we are doing is generating a series of numbers based off the users input on how many numbers to generate and what the maximum number can be. The numbers are ordered to be ascending. Lastly we output the data to the textbox. Each time you click Generate Numbers a new set of numbers are selected. Clicking reset clears the text box back to its original state. There are also fixes for the errors thrown when using Javascripts document commands in Netscape.

What it does:
Generates X number of numbers.
Value of each number cannot exceed Y.
Verify no repeat numbers.
Output to text box.

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

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

<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="generatenos()" 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. <script language="JavaScript">
  9. var loop;
  10. var randy;
  11. function make_array(size){
  12. this.length = size;
  13.       for (a=0; a<size; a++) this[a]="";
  14. }
  15. function generatenos(){
  16. hold = new make_array(document.lottery.max_number.value);
  17. document.lottery.numbers.value = "";
  18.     for(loop=0;loop<document.lottery.balls_drawn.value;loop++){
  19.         randy = Math.round(Math.random()*(document.lottery.max_number.value-1));
  20.         randy++;
  21.         if(hold[randy]==1){
  22.             loop--;
  23.             continue;
  24.             }
  25.         if(hold[randy]!=1){
  26.             hold[randy]=1;
  27.             }
  28.             }
  29.     for(loop=0;loop<=document.lottery.max_number.value;loop++){
  30.         if(hold[loop]==1)
  31.             document.lottery.numbers.value = document.lottery.numbers.value + loop + " ";
  32.     }
  33. }
  34. </script>
  35. <script language="JavaScript">
  36. var axel = Math.random() + "";
  37. var PageId = axel * 1000000000000000000;
  38. NS4 = document.layers;
  39. if (NS4) {
  40. origWidth = innerWidth;
  41. origHeight = innerHeight;
  42. }
  43. function reDo() {
  44. if (innerWidth != origWidth || innerHeight != origHeight)
  45. location.reload();
  46. }
  47. if (NS4) onresize = reDo;
  48. </script>
  49. <center>
  50. <form name="lottery">Total number of balls:
  51.    <input type="text" name="balls_drawn" size="3" value="6">
  52.    Maximum Number:
  53.    <input type="text" name="max_number" size="3" value="49">
  54.    <input type="button" value="Generate Numbers" onclick="generatenos()" name="button2"><br />
  55.    <input type="text" name="numbers" size="29" value="Good luck!">
  56.    <input type="reset">
  57.    </form>
  58. </tr>
  59. </tbody></table></center>
  60. </body>
  61. </html>


Hopefully this does what you want it to do. Post up if you still have issues!
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Poly

Tested your script and it works great from what I have done so far.

I want to thank you very much for all your assistance.

Since I know what the script does, I can use it as a valuable learning execise.

Again, thank you very much for your help.

Could not have done it without you

Are you a night owl as you seem to be up in the wee hours of the morning

Joe
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

Night time is the best time of the day ;)
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Poly

As I was testing, I accidentally left the max balls and max number blank and as expected nothing happened.

Is it possible to add an alert message such that if either or both are left blank to say something like:

max balls: "You must enter the number of balls"

max number: "You must enter the maximum number and it must be greater than the number of balls"

Thanks
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Poly

I noticed that if I filled in the number of balls but left the max number blank, the script went into an endless loop.

However, if I left the number of balls blank, but filled in the max number then nothing happened.

I guess then the critical one is the number of balls.

If it is left blank, the you should get the alert message and the script should be prevented from running or else just reset to beginning after ok is pressed on the alert message.

For the max number I guess you only need the alert message

Joe
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

I'll fix it up first thing in the AM. Will be a simple JS validation. One question on deciding how to do it: If I build it using an external .js file for validation, can you use that?
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

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
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Poly

external .js file would be a problem for me.

please try to do everything inside script

Thanks
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

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>
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Poly

Works Great!

You the man

Thanks again for all your help

Joe
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

Not a problem.
  • aspiringpro
  • Born
  • Born
  • aspiringpro
  • Posts: 1

Post 3+ Months Ago

Just one little problem. The bonus number should be able to be a duplicate of any of the regular numbers because it is indeed a separately drawn number. Your code does not allow for that. For instance, in the Powerball drawing they draw five white balls out of a drum with 59 balls and one red ball out of a drum with 35 red balls.

Post Information

  • Total Posts in this topic: 21 posts
  • Users browsing this forum: Bogey, Liamw411 and 54 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.