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.

Help me keep writing

Every coffee gives me a push toward the next article.

Comments

There are no comments yet.

You may also like