The random numbering of locomotives by many, if not most, of the pre-grouping companies, and reuse of the lower numbers, seems strange; as far as I can make out from odd comments it seems to have been driven by accountants in their calculations for depreciation and valuing assets. It was not uncommon for a railway which had say 97 locomotives to order 6 more, three of them numbered 12, 23 and 9, being numbers recently withdrawn, and the other three numbered 101-103 as an extension of the numbering scheme.
The first locos were sometimes known as "replacements", although mechanically they were no different to the other three. In accounting minutes, you may find that the cost of the first three was charged to the general expenses account (and not depreciated), and the second three were charged to additional capital of the railway (and depreciated), and quite possibly additional shares would be sold to (in theory) add to the asset base of the company. I wonder if there was some aspect of tax calculations that led to this approach.
All sorts of peculiar variations on this theme happened. In some cases where a loco had been withdrawn one of the latest ones would be renumbered downwards to fill the gap, as if there was some fear of missing numbers. Some railways renumbered locos anyway after say 30 years into a "duplicate list", say by adding an A to the number, which allowed it to be reused for a new build, the older loco being theoretically at the end of its life. There are extreme cases where the pre-grouping company did this to a loco number which was still happily in stock and in use after nationalisation! I can recall from somewhere that some "replacements" were built in the railway's own workshops, while the otherwise identical new capital stock ones were built by outside contractors.