Pin Me

Use JavaScript to Pick Daily Lottery Numbers

written by: cra8051•edited by: Michele McDonough•updated: 8/14/2010

Anyone who plays a daily lottery game probably realizes that no system can promise a winning combination because of the randomness of lottery picks. Some play these games based on meaningful combinations of numbers such as the birthdays of relatives and friends. Here is a different way to select lo

  • slide 1 of 3

    Setup the Game Parameters

    In states with daily lottery games, the player must pick three lottery numbers between 0 and 9, although the same number may be used more than once. In many states there are multiple games within this system, for example, winning front or back pairs can win some (Example: 338 or 438). To select numbers based on random numbers, create a simple Javascript in an HTML file using Notepad or similar text editor. It only takes a few lines for this part. Type in the following code and save it with the name “lotto" and an HTML or HTM extension instead of the default txt type.

    *****<SCRIPT LANGUAGE="JavaScript">

    function lotto()





    alert("Your Lotto numbers for today are: " +nbr1+ " " +nbr2+" "+nbr3+ " .")



    <INPUT TYPE="button" VALUE="Give me my winning numbers!" onClick="lotto()">


    Locate this file on your computer and right click on it. Select “Open with" and either Firefox or Internet Explorer. You should see the button. When you click on this, you will get three random numbers for the lottery.

  • slide 2 of 3

    Lottery Number Picker

    screenshot by C.R. AndersonResults of pick
  • slide 3 of 3

    How It Works

    The first line of the script creates a function named “lotto." The three daily lottery number possibilities are variables nbr1, nbr2 and nbr. Math.floor is one of JavaScript’s mathematics functions, which rounds a fractional number to the nearest integer. Math.random generates a random number between .000 and .999. To ensure no returned number is greater than 9, multiply the result in each case by 9. Finally, define a form with a single button using the text “Give me my winning numbers!" You can use any trigger words you want of course. The “onClick" event calls the lotto function. The “alert" command displays the results.

    If the daily game in your state does not use zero, increase each number’s final result by setting the nbr value to nbr + 1, that is, nbr1=nbr1+1 and so forth.

    If you want to mount this little program on your website, just add these lines to the beginning of the code:



    Add the closing tags at the end of the Javascript:



    You can set up the same kind of number generator for weekly games by modifying the above code. Create additional number variables for all the numbers drawn. Depending on the range of numbers for the weekly draw, multiply the random numbers by the maximum numbers allowed. If zero is not one of the allowed numbers, then add 1 to each number.

More To Explore