Search code examples
arrayspowershellrandomsequential-number

Generate unique semi random numeric strings


I want to generate 10k of unique numbers composed of a sequential number (16 digits) followed by a random numeric string (4 digits). Weapon of choice is Powershell because it's the only tool I have a very limited knowledge of.

Problems encountered:

  1. Generating 10k sequential numbers using the following method: 1400000000000000..1400000000010000 and putting into a variable. Error: Value is to large for an Int32
  2. Generating 10k of 4 digit via Get-Random -Minimum 0001 -Maximum 9999 and putting them in a variable. I manage only to obtain 1 random number.
  3. Combining the two variables using the Join-Object (or at least that's what I hope could be done)

How can I combine these 3 commands to obtain a list of semi random numbers as specified above? Or is there simpler way to achieve the same result?


Solution

  • (0..10000).ForEach{ '14000000000{0:00000}{1:0000}' -f $_, (Get-Random 10000) }
    

    Result:

    14000000000000004965
    14000000000000010114
    14000000000000026382
    14000000000000038644
    14000000000000045435
    14000000000000052051
    14000000000000061801
    14000000000000077046
    14000000000000087791
    14000000000000098090
    14000000000000102712
    ....
    

    Explanation:

    • Use the Format Operator (-f) to format the string, like '14000000000{0:00000}{1:0000}'. For details, see composite formatting.
    • You do not have to put the whole 16 digit number in a [int32], just count from 0..1000 with leading zeros up to 5 digits ({0:00000}) and prefix it with 14000000000
    • In the second place holder ({1:0000}), place a new random number with leading zeros up to 4 digits