V
V
Vlad_Radigin2018-05-07 17:18:38
Java
Vlad_Radigin, 2018-05-07 17:18:38

How to make a generator of random letters and numbers in java?

How to make a generator of random numbers and letters in java?
For example, so that each time, completely random numbers are displayed, and letters, for example 8, or 16, random numbers? and letters?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2018-05-07
@Vlad_Radigin

int length = 16;
Random r = new Random();
String s = r.ints(48, 122)
            .filter(i -> (i < 57 || i > 65) && (i < 90 || i > 97))
            .mapToObj(i -> (char) i)
            .limit(length)
            .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
            .toString();

T
Therapyx, 2018-05-07
@Therapyx

So I think it will be clearer.

import java.util.Random;

char[] array = new char[8];
int rand;
Random r = new Random();
for (int i = 0; i< 8; i++) {
     rand = r.nextInt(127) + 1; //тут менять нужные диапазоны ((max - min) + 1) + min (см ASCII)
     array[i] = (char)rand;
}
for(char c : array) 
     System.out.println(c);
}

ASCII

M
MaxLich, 2018-05-08
@MaxLich

You can also create an array or a string with all the listed letters and numbers. And already randomly select an element of this array or string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question