How to generate effectively a random number that only contains unique digits in Python?
<pre><code>import random

def get_number(size):
  result = [random.randint(1,9)]
  digits = list(range(0,10))
  digits.remove(result[0])
  if(size &gt; 1):
    result += random.sample(digits,size-1)
  return ''.join(map(str,result))

print(get_number(4))
</code></pre>

<p>I solved the problem, but I feel that it's clumsy. 
How can I do this more effectively and more elegant? </p>

<p>Shuffle is the way to go as suggested by @jonrsharpe:</p>

<pre><code>import random

def get_number(size):
    l = [ str(i) for i in list(range(10))]
    while l[0] == '0':
        random.shuffle(l)
    return int("".join(l[:size]))
</code></pre>

<p>Limits:</p>

<ul>
<li>is you ask for a number of more than 10 digits, you will only get 10 digits</li>
<li>it can take some steps if first digit is initially a 0</li>
</ul>

python list random