Last active
August 29, 2015 14:07
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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