Lottery Number Generator

A HunBug Document
Document No.: 11190
Last Updated: 01 Jan 2006
Author: HunBug
Have difficulty choosing your lottery number? This solution is based on the UK National Lottery, which has draws every Wednesday and Saturday, which generates a set of numbers based on the date of the next draw, rather than just random numbers.

Javascript Code
In this solution, the Javascript is included in the HTML code. The HTML is extremely basic, so not to distract from the Lottery number generation.
<html>

  <script type="text/javascript">
    function sortNumber(a,b)
    {
      return a - b
    }
  </script>
 
  <body>
 
    <h1> Lottery Numbers </h1>
    
    <script type="text/javascript">
      // Get the date of the next draw
      var date  = new Date();
      var day   = date.getDay();
      date.setDate(date.getDate());
      draw=date;
      if(date.getDay() < 4)
        draw.setDate(date.getDate()+(3-date.getDay()));
      else
        draw.setDate(date.getDate()+(6-date.getDay()));
      document.write('Next draw is on '+draw.toString().substring(0,10)+'<br><br>');
      
      // Generate the lottery numbers
      var dd=draw.getDay()+14;
      var dm=draw.getMonth()+41;
      var balls=new Array();
      balls[0] = Math.floor((dd/dm*30)%49)+1;
      balls[1] = Math.floor((dd/dm*420)%48)+1;
      balls[2] = Math.floor(((dd%4)*(dd*dm-30*2))%47)+1;
      balls[3] = Math.floor((Math.pow(dd,dm))%46)+1;
      balls[4] = Math.floor((Math.abs(Math.tan(dd+428))*4444)%45)+1;
      balls[5] = Math.floor((dm+41221*dd)%44)+1;
      for(b=1;b<=5;b++)
      {
        var i=0;
        for(i=0;i<b;i++)
          if(balls[b]>=balls[i])
            balls[b]=balls[b]+1;
      }
      balls.sort(sortNumber);

      // Write the lottery numbers
      document.write('Lottery Numbers : ');
      for(b=0;b<=5;b++)
        document.write(balls[b]+', ');
        
    </script>
 
  </body>
 
</html>

HTML Output
When the HTML is displayed, the next draw date is displayed, along with the six lottery numbers. The six numbers will always be the same for the next draw date.
Lottery Numbers
Next draw is on Wed Jul 12

Lottery Numbers : 6, 13, 22, 23, 30, 40,

How It Works
The Javascript can be split into three sections.
The first section calculates the date of the next draw.
var date  = new Date();
var day   = date.getDay();
date.setDate(date.getDate());
draw=date;
if(date.getDay() < 4)
  draw.setDate(date.getDate()+(3-date.getDay()));
else
  draw.setDate(date.getDate()+(6-date.getDay()));
document.write('Next draw is on '+draw.toString().substring(0,10)+'<br><br>');
If the day is less than 4, the next draw will be on a Wednesday; otherwise the draw will be on a Saturday. The draw variable is then set to the draw date, which is written out to the document

The second section generates the numbers. So that the numbers are always the same for the each draw date, values from the draw date are used as the seeds for the calculations.
var dd=draw.getDay()+14;
var dm=draw.getMonth()+41;
var balls=new Array();
balls[0] = Math.floor((dd/dm*30)%49)+1;
balls[1] = Math.floor((dd/dm*420)%48)+1;
balls[2] = Math.floor(((dd%4)*(dd*dm-30*2))%47)+1;
balls[3] = Math.floor((Math.pow(dd,dm))%46)+1;
balls[4] = Math.floor((Math.abs(Math.tan(dd+428))*4444)%45)+1;
balls[5] = Math.floor((dm+41221*dd)%44)+1;
The calculations are designed to give widely varying answers, from draw to draw. The initial result is put into a calculation with the modulus of the number of balls available, plus one. These calculations can be replaced with randomly generated numbers, if they don’t need to be the same for each draw.
for(b=1;b<=5;b++)
{
  var i=0;
  for(i=0;i<b;i++)
    if(balls[b]>=balls[i])
      balls[b]=balls[b]+1
}
balls.sort(sortNumber);
To make sure none of the numbers have been replicated, we loop through the results. If a ball with a number less than or equal to the ball we are checking, has been chosen, we add one to the result. Thus ensuring no number is selected multiple times and all numbers have an equal chance of selection.

The results are then output to the document, giving you a prediction for the next draw.

Further Reading


National Lottery: The National Lottery Homepage
Buddy’s Prediction: Themed lottery number prediction page

HunBug Store








Comments are all individually read, none are automatically posted to the site.