Menu Home

Generating lotto numbers

A client joked today about predicting lotto numbers. This post doesn’t claim to do that, obviously, but I realised that I can use my rand script to generate some random lotto numbers. Obviously the below adheres to the NZ Lotto format of 6 numbers under 40, a bonus ball under 40 and a power ball under 10.

I started off with a couple of one-liners and then refined it out to a shell loop, like so:

printf "%s\n" \
  "                  |Bonus|Power" \
  "                  |Ball |Ball" \
  "------------------------------"

# 10 iterations
for _ in {1..10}; do
  # Generate an array of 6 random numbers, sort and uniq them to dedupe 
  # use sed to zero-fill any single digit numbers
  numArray=( $(rand -M 40 -c 6 | sed 's/\< [0-9]\>/0&/' | sort -n | uniq) )
  # Generate a random number under 40 for our bonus ball
  bonusBall=$(rand -M 40 | sed 's/\< [0-9]\>/0&/')
  # Generate a random number under 10 for our power ball
  powerBall=$(rand -M 10 | sed 's/\< [0-9]\>/0&/')

  # If our array doesn't have 6 unique numbers, we add to it
  while [[ "${#numArray[@]}" -lt 6 ]]; do
    randNum=$(rand -M 40 | sed 's/\< [0-9]\>/0&/')
     
    if [[ ! "${numArray[@]}" =~ ${randNum} ]]; then
      numArray+=(${randNum})
    fi
  done

  # Now we sort the array one more time
  sortNumArray=( $( printf "%s\n" "${numArray[@]}" | sort -n ) )

  # Next, we check that the bonus ball is not in the array
  while [[ "${numArray[@]}" =~ ${bonusBall} ]]; do
    bonusBall=$(rand -M 40 | sed 's/\< [0-9]\>/0&/')
  done

  # Finally we print the line
  printf "%s\n" "${sortNumArray[*]} | ${bonusBall} | ${powerBall}"
done

So essentially we create 6 random integers between 01 and 40, and we store them in an array. We sort and uniq to ensure that they’re unique (as the rand script will allow duplicates by design). In case we don’t generate 6 unique random integers, we top up the array and resort. We have another random number as our bonus ball, so we check that it doesn’t match the array, if so we regenerate it. Then we print the lot along with another random integer between 01 and 10 for the power ball. We also throw in sed to zero-pad any numbers under 10 (i.e. ‘9’ becomes ’09’)

Performance could probably be improved by using printf‘s -v option, but that does reduce portability significantly.

Because the lotto ticket I usually order (on the very rare occasion I have money that I can afford to lose) has 10 lines, I’ve defaulted to that.

                  |Bonus|Power
                  |Ball |Ball
------------------------------
01 03 04 28 35 36 | 27 | 05
10 25 28 37 38 40 | 08 | 09
14 15 20 34 37 40 | 39 | 09
08 10 11 12 22 31 | 02 | 03
02 10 26 30 31 39 | 36 | 08
02 11 17 23 30 37 | 13 | 09
11 16 18 21 25 39 | 29 | 09
08 10 14 26 36 40 | 17 | 04
04 06 10 15 21 25 | 24 | 07
01 08 20 21 33 38 | 18 | 02

Legal stuff: I don’t know if you’re allowed to use an xRNG to choose your own lotto numbers. Check with your local lotteries organisation about their rules!

Categories: Geeking Out Random

rawiri