package shop.alien.util.common;

import java.util.HashSet;
import java.util.Random;

public class UniqueRandomNumGenerator {

    public static String generateUniqueCode(int length) {
        HashSet<String> uniqueCodes = new HashSet<>();
        Random random = new Random();
        String code;

        do {
            StringBuilder codeBuilder = new StringBuilder(length);

            for (int i = 0; i < length; i++) {
                codeBuilder.append(random.nextInt(10));
            }
            code = codeBuilder.toString();
        } while (!uniqueCodes.add(code));

        return code;
    }
}
