1

I want to generate a random 4 digit number in which none of the digits are repeated.

 import random

 sets = random.sample(range(0,9), 4)

This generates a random set of 4 digits but I want this as an integer. how do I do that?

Varad
  • 920
  • 10
  • 25
Izzy
  • 11
  • 1

7 Answers7

3

(Assuming OP meant all the digits)
Instead of using numbers and have to manipulate to str and back to int, just start with ascii digits:

>>> import string
>>> ''.join(random.sample(string.digits, 4))
'4561'

You can convert to int() if necessary.
It's unclear what the OP intends to do if the first digit is 0.

For a purely numerical approach you can use functools.reduce:

>>> import functools as ft
>>> ft.reduce(lambda s, d: 10*s + d, random.sample(range(10), 4))
2945
AChampion
  • 29,683
  • 4
  • 59
  • 75
1

You can do this by converting each digit to a string, joining them, and casting them as an integer.

int("".join(map(str,random.sample(range(0,9),4))))
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
1

if you need to generate 4 digit number, just for knowledge purpose use.

As suggested by AChampion this solution can contain duplicates

from random import randint randint(1000, 9999)

Use bernie Solution to generate a random 4 digit number in which none of the digits are repeated.

int("".join(map(str,random.sample(range(0,9),4))))
JkShaw
  • 1,927
  • 2
  • 13
  • 14
1

In case if you want potentially infinite sequence of numbers with 4 unique digits (or any other condition – write your own)

import random


def numbers_gen(left_end, right_end):
    while True:
        yield random.randint(left_end, right_end)


def are_digits_unique(number):
    number_string = str(number)
    return list(set(number_string)) == list(number_string)


four_digits_numbers_gen = number_gen(left_end=1000,
                                     right_end=9999)
four_digits_numbers_with_unique_digits_gen = filter(are_digits_unique,
                                                    four_digits_numbers_gen)

Works only in Python 3 because filter returns iterator-object (in Python 2.7 it returns list, more at docs)

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
0

You can multiply with powers of 10:

sum(10**a*b for a, b in enumerate(reversed(sets)))

This works as long as the first element of sets is not zero.

Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
  • Is `reversed()` necessary given they are just random digits? – AChampion Apr 12 '17 at 05:15
  • @AChampion Depends on the nitty-gritty, doesn't it? For example, if upstream were to special case the first digit to be nonzero it would certainly be a nice touch to make sure it ends up on the left. – Paul Panzer Apr 12 '17 at 05:24
  • True, good point. Why do you claim this does not to work if the first element is `0`. Given you are reversing it, should give `XXX0` and if the last item is `0` it just gives a 3 digit number (implied `0`) – AChampion Apr 12 '17 at 05:29
  • @AChampion Hm, doesn't it construct the number lsd and therefore rightmost first? I think I even tested it. -- Very commendable Horner scheme in your answer, btw. – Paul Panzer Apr 12 '17 at 05:38
  • Sorry, you are right `3` digit result occurs on first digit `0`. Unclear what the OP intends to do with first digit `0`. – AChampion Apr 12 '17 at 05:42
0

You could try:

import random
my_set = set()
while len(my_set) < 4:
    x = random.choice(range(0,9))
    my_set.add(x)
my_num = int("".join(map(str, my_set)))
cullywest
  • 121
  • 4
0

One line:

int("".join(random.sample("0123456789",4)))
Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24