title: Python - How to always generate valid Luhn numbers?
body: <p><strong>I'm trying to generate valid Luhn numbers using random library</strong> (without using other libraries like fast_luhn)<br /><br /> I generated a random number, then the last digit is iterated from 0 to 9, each iteration being a new case to check in the function <code>valid_luhn</code>, and then the same with the next digit of the first number generated, but I got stuck with this exponential, random and branched idea.<br /><br /></p>
  
  <p>This is my code to validate the numbers using Luhn algorithm (if you have any suggetion to improve it, like use a better list comprehension, let me know):</p>
  
  <pre><code>def valid_luhn(cc):
      num = list(map(int, str(cc)))
      return sum(num[::-2] + [sum(divmod(d * 2, 10)) for d in num[-2::-2]]) % 10 == 0
  </code></pre>