Script en Python para generar DNI aleatorio

Cuando realizo testing, o debo generar mucha información falsa para rellenar una base de datos, me encuentro con la necesidad de crear DNIs españoles que sean válidos. El siguiente código en Python genera aleatoriamente los números, con la letra de control correspondiente.

from random import randint

POSSIBLE_LETTERS = (
    "T",
    "R",
    "W",
    "A",
    "G",
    "M",
    "Y",
    "F",
    "P",
    "D",
    "X",
    "B",
    "N",
    "J",
    "Z",
    "S",
    "Q",
    "V",
    "H",
    "L",
    "C",
    "K",
    "E",
    "T",
)
NUMBER_DNI = randint(10000000, 99999999)
LETTER_DNI = POSSIBLE_LETTERS[NUMBER_DNI % 23]
print(f"{NUMBER_DNI}{LETTER_DNI}")

Espero que os sea de utilidad.

This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.

Will you buy me a coffee?

You can use the terminal.

ssh customer@andros.dev -p 5555

Written by Andros Fenollosa

November 30, 2021

1 min of reading

You may also like