Generate Constrained Number Sequences in Python

Some applications require generating a number sequences that must fulfill multiple constraints. I solved this problem with a python script and a couple of functions using lists.

I was interested in generating 10-digit numbers that met the following conditions:

  • digits 0 and 1 (from left to right) are random from 01 to 25
  • digit 2 is < 7
  • digits 3 to 8 are random

Digit 9 is a checksum digit. It is calculated by looking at the first 9 digits to the left. Digits on even positions are doubled. If the product is larger than 9, we keep the difference. Digits on odd positions are kept as is or multiplied by one. The sum of the products is subtracted for the nearest tenth. This becomes the checksum digit.

My code has three functions or sections:

Some of the more interesting bits of code, are to create a random six-digit number sequence

The checksum function is as follows:


Leave a Comment

Required fields are marked *