Languages
[Edit]
PL

Java - generuj losow─ů tablic─Ö znaków o rozmiarze 10

3 points
Created by:
Sylwia
1334

1. Wygeneruj losow─ů tablic─Ö znak├│w o rozmiarze 10 za pomoc─ů┬áRandom

public static char[] randomCharArray(int len) {
	String alphabet =
			"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
			"abcdefghijklmnopqrstuvwxyz" +
			"0123456789";

	StringBuilder b = new StringBuilder();

	for (int i = 0; i < len; i++) {
		int randIdx = new Random().nextInt(alphabet.length());
		char randChar = alphabet.charAt(randIdx);
		b.append(randChar);
	}

	return b.toString().toCharArray();
}

Przykład:

// [v, K, 5, f, H, R, k, j, O, K]
System.out.println(Arrays.toString(randomCharArray(10)));

// [h, U, x, V, u, P, C, c, 1, I]
System.out.println(Arrays.toString(randomCharArray(10)));

// [F, Z, p, i, 0, d, O, X, a, B]
System.out.println(Arrays.toString(randomCharArray(10)));
Hey ­čĹő
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.