Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random Lotto Number Generator

    I've sort of got stuck with my random lotto number generator and need a helpful nudge. If anyone has got some helpful insights to get my lotto number generator to work, that would be awesome.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!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>
    <title>Lottery Number Generator</title>
    <script language="JavaScript" type="text/javascript">
    
    function Lottery() {
    this.balls = new Array();
    this.freshSet = freshSet;
    this.selectOneNumber = selectOneNumber;
    }
    
    function freshSet(maxNumber) {
    maxNumber=parseInt(maxNumber);
    if( isNaN(maxNumber) || maxNumber<0 ) maxNumber=0;
    // create new set of balls numbered from 1 to maxNumber inclusive
    
    for (var k=0; k<maxNumber; k++) this.balls[k] = k+1;
    }
    
    function selectOneNumber() {
    if( this.balls.length<1 ) return -1;
    var i = Math.random()*this.balls.length;
    i = Math.floor(i);
    return this.balls.splice(i,1);
    }
    
    function Lotto()
    {
    
    var newballs = Lottery
    
    
    alert ("The function was called")
    return false;
    
    }
    // THIS SECTION OF CODE (ABOVE) MUST BE USED WITHOUT MODIFICATION
    
    //]]>
    </script>
    </head>
    
    <body bgcolor="#FFFFFF">
    <h2>Lottery Number Generator</h2>
    
    <form method="get" action="" onsubmit="return Lotto();" name="lotto" id="numbers">
    <table border="0">
    <tr>
    <th align="right">Your Lotto Numbers</th>
    
    <td><input type="text" size="2" name="num" /></td>
    <td><input type="text" size="2" name="num" /></td>
    <td><input type="text" size="2" name="num" /></td>
    <td><input type="text" size="2" name="num" /></td>
    <td><input type="text" size="2" name="num" /></td>
    <td><input type="text" size="2" name="num" /></td>
    </tr>
    
    
    
    <td><input type="submit" value="Roll Those Numbers" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    Last edited by vinyl-junkie; 05-27-2013 at 04:28 PM. Reason: added code tags

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Is that the original state of the template you were handed, or have you added to it?

    If all you're doing is displaying the numbers, then there's no real need even to store them.

    Code:
    <script  type="text/javascript">
    
    function Lottery( count, max ) 
    {
    
     this.balls = new Array();
     this.freshSet = freshSet;
     
     this.freshSet( count, max );
    }
    
    function freshSet( ballCount, maxNumber ) 
    {
      var ballSet = [];
      
      for (var k=0; k<maxNumber; k++ ) 
        ballSet[k] = k+1;
         
      for( var i = 0; i < ballCount; i++ )
        this.balls.push( ballSet.splice( Math.floor( Math.random() * ballSet.length ), 1 )[0] );  
                          
      alert(this.balls);  
    }
    
    function Lotto()
    {
     var newballs = new Lottery( 6,49 );
    
     return false;
    }

  • #3
    New to the CF scene
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah - that would be very inefficient. /face palms self


  •  

    Posting Permissions

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