Hi,
Id like to generate a nine-digit combinations of 52 characters of alphabet (a..z and A..Z).

My thought is:

  1. Generate nine-digit combinations of numbers from 0 to 51
  2. Use these combinations as indexes of
    char[] alp={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

Iam sure its very easy..
Im learning Java for one month (once a week), sorry for my english (:
Thank you very much for any idea

Dani AI

Generated

As noted in a later post, the figure 3,679,075,400 matches C(52,9) — that is the number of 9-element combinations chosen from 52 letters when order does not matter and no letter repeats. For clarity: ordered 9-character strings without repetition give P(52,9) = 1,335,062,881,152,000, and allowing repetition gives 52^9 = 2,781,005,885,635,712. Which of those three is intended determines the algorithm and feasibility.

Random picks (as suggested) are fine for sampling, but a systematic, non-repeating enumeration is best done by treating each output as either (a) a k-combination from n items (unordered, no repeats) or (b) a k-length base-n counter (ordered, repeats allowed). For the combination case (C(52,9)), a compact, memory-light approach is lexicographic generation with an int[] of indices; no global “removal” of characters is required — every combination is produced from index positions that are updated in place.

A minimal Java sketch for lexicographic k-combination generation (n=52, k=9) and streaming to disk:

String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int n = 52, k = 9;
int[] comb = new int[k];
for (int i = 0; i < k; i++) comb[i] = i; // start: 0..k-1

// advance to next lexicographic combination; returns false when finished
public static boolean nextCombination(int[] comb, int n) {
    int k = comb.length;
    for (int i = k - 1; i >= 0; i--) {
        if (comb[i] < n - k + i) {
            comb[i]++;
            for (int j = i + 1; j < k; j++) comb[j] = comb[j - 1] + 1;
            return true;
        }
    }
    return false;
}

// inside a try-with-resources BufferedWriter: create a char[] from comb and write,
// then call nextCombination(comb, n) to iterate through all C(n,k) combinations.

Practical notes and cautions: C(52,9) exceeds Integer.MAX_VALUE, so counters and ranks must use long. Streaming 3,679,075,400 lines of 9 chars plus newline is roughly 36–40 GB; generation time will be IO-bound unless heavily optimized (estimates: at 1,000,000 combos/sec about 1 hour). For parallel generation, partition the rank range and use combinatorial unranking (binomial coefficients) to compute each thread’s start combination so each worker enumerates a disjoint block. Compression, large buffers, and avoiding in-memory accumulation are essential.

Recommended Answers

All 4 Replies

Member Avatar for Member #682468

That is a good start, you now just need to generate a random number between 0 and 51, this can be achieved by:

import java.lang.Math;

int a = (int) Math.random() * 52; //Create a random number between 0 and 51

Then use the variable a as an array index.

That is a good start, you now just need to generate a random number between 0 and 51, this can be achieved by:

import java.lang.Math;

int a = (int) Math.random() * 52; //Create a random number between 0 and 51

Then use the variable a as an array index.

Thanks but I have to systematically generate combinations with no recurrence.

Member Avatar for Member #682468

You could still use random, but use an ArrayList and delete the occurrence after it has been used.

You could still use random, but use an ArrayList and delete the occurrence after it has been used.

But it must generate 3679075400 nine-digit combinations.I cant get rid of characters becose I have to use it in next generated combination..but thank you anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.