Skip to content

Instantly share code, notes, and snippets.

@vladholubiev
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save vladholubiev/9799d5c9e304b15a47a5 to your computer and use it in GitHub Desktop.

Select an option

Save vladholubiev/9799d5c9e304b15a47a5 to your computer and use it in GitHub Desktop.
This file shows some examples how to generate string filled with random chars.
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
java7Way();
java8Way();
java8AlphabetWay();
}
public static void java7Way() {
char[] arr = new char[60];
Random rand = new Random();
for (int i = 0; i < 60; i++) {
int r = rand.nextInt(136);
if (r >= 65) {
arr[i] = (char) r;
} else i--;
}
System.out.println(arr);
}
public static void java8Way() {
char[] arr = new char[60];
List<Character> list = new ArrayList<>();
new Random().ints(60, 65, 123).forEach(e -> list.add((char) e));
IntStream.range(0, 60).forEach(e -> arr[e] = list.get(e));
System.out.println(arr);
}
public static void java8AlphabetWay() {
IntStream.range(0, 60).forEach(e -> System.out.print("абвгдеёжзийклмнопрстуфхцчшщъыьэюя".charAt(new Random(e).nextInt(33))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment