Java 范围为 (A..Z, 0..9) 和标点符号的随机字符生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3114606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random character generator with a range of (A..Z, 0..9) and punctuation
提问by jadkins4
I need to create a random character generator that return a single character. The character should range within the letters of the alphabet, numbers 0 through 9, and some characters like ,.?/-. Any example would be appreciated.
我需要创建一个返回单个字符的随机字符生成器。该字符的范围应在字母表、数字 0 到 9 以及一些字符(如 ,.?/-)内。任何例子将不胜感激。
回答by jadkins4
Pick a random number between [0, x), where x is the number of different symbols. Hopefully the choice is uniformly chosen and not predictable :-)
Now choose the symbol representing x.
Profit!
在 [0, x) 之间选择一个随机数,其中 x 是不同符号的数量。希望选择是统一选择的,不可预测:-)
现在选择代表 x 的符号。
利润!
I would start reading up Pseudorandomnessand then some common Pseudo-random number generators. Of course, your language hopefully already has a suitable "random" function :-)
我会开始阅读Pseudorandomness,然后是一些常见的Pseudo-random number generators。当然,你的语言希望已经有一个合适的“随机”功能:-)
回答by Nicolas Viennot
Using some simple command line (bash scripting):
使用一些简单的命令行(bash 脚本):
$ cat /dev/urandom | tr -cd 'a-z0-9,.?/\-' | head -c 30 | xargs
t315,qeqaszwz6kxv?761rf.cj/7gc
$ cat /dev/urandom | tr -cd 'a-z0-9,.?/\-' | head -c 1 | xargs
f
- cat /dev/urandom: get a random stream of char from the kernel
- tr: keep only char char we want
- head: take only the first
n
chars - xargs: just for adding a
'\n'
char
- cat /dev/urandom:从内核获取随机字符流
- tr:只保留我们想要的字符
- head:只取第一个
n
字符 - xargs:仅用于添加
'\n'
字符
回答by polygenelubricants
The easiest is to do the following:
最简单的方法是执行以下操作:
- Create a
String alphabet
with the chars that you want. - Say
N = alphabet.length()
- Then we can ask a
java.util.Random
for anint x = nextInt(N)
alphabet.charAt(x)
is a random char from the alphabet
String alphabet
用你想要的字符创建一个。- 说
N = alphabet.length()
- 然后我们可以要求
java.util.Random
一个int x = nextInt(N)
alphabet.charAt(x)
是字母表中的随机字符
Here's an example:
下面是一个例子:
final String alphabet = "0123456789ABCDE";
final int N = alphabet.length();
Random r = new Random();
for (int i = 0; i < 50; i++) {
System.out.print(alphabet.charAt(r.nextInt(N)));
}
回答by sanssan
Here is code for secure, easy, but a little bit more expensive session identifiers.
这是用于安全、简单但稍微贵一点的会话标识符的代码。
import java.security.SecureRandom;
import java.math.BigInteger;
public final class SessionIdentifierGenerator
{
private SecureRandom random = new SecureRandom();
public String nextSessionId()
{
return new BigInteger(130, random).toString(32);
}
}
回答by Danubian Sailor
Why reinvent the wheel? RandomStringUtils from Apache Commons has functions to which you can specify the character set from which characters are generated. You can take what you need to your app:
为什么要重新发明轮子?来自 Apache Commons 的 RandomStringUtils 具有一些函数,您可以在其中指定生成字符的字符集。你可以把你需要的东西带到你的应用程序中:
http://kickjava.com/src/org/apache/commons/lang/RandomStringUtils.java.htm
http://kickjava.com/src/org/apache/commons/lang/RandomStringUtils.java.htm
回答by duggu
See below link : http://www.asciitable.com/
请参阅以下链接:http: //www.asciitable.com/
public static char randomSeriesForThreeCharacter() {
Random r = new Random();
char random_3_Char = (char) (48 + r.nextInt(47));
return random_3_Char;
}
Now you can generate a character at one time of calling.
现在您可以在一次调用时生成一个字符。
回答by Ben Patterson
Random random = new Random();
int n = random.nextInt(69) + 32;
if (n > 96) {
n += 26;
}
char c = (char) n;
I guess it depends which punctuation you want to include, but this should generate a random character including all of the punctuation on this ASCII table. Basically, I've generated a random int from 32 - 96 or 123 - 126, which I have then casted to a char, which gives the ASCII equivalent of that number. Also, make sure youimport java.util.Random
我想这取决于您要包含哪个标点符号,但这应该会生成一个随机字符,包括此 ASCII 表上的所有标点符号。基本上,我从 32 - 96 或 123 - 126 生成了一个随机整数,然后我将其转换为一个字符,它给出了该数字的 ASCII 等效值。另外,请确保您import java.util.Random
回答by Ben Patterson
You should first make a String that holds all of the letters/numbers that you want.
Then, make a Random. e. g. Random rnd = new Random;
Finally, make something that actually gets a random character from your String containing your alphabet.
For example,
您应该首先制作一个包含您想要的所有字母/数字的字符串。
然后,做一个随机。例如,Random rnd = new Random;
最后,制作一些实际上从包含字母表的字符串中获取随机字符的东西。
例如,
import java.util.Random;
public class randomCharacter {
public static void main(String[] args) {
String alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ?/.,";
Random rnd = new Random();
char char = alphabet.charAt(rnd.nextInt(alphabet.length()));
// do whatever you want with the character
}
}
See this.
It's where I got this info from.
看到这个。
我就是从那里得到这些信息的。
回答by ahmad apandi
random char package com.company;
com.company 随机字符包;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
public static void main(String[] args) {
// write your code here
char hurufBesar =randomSeriesForThreeCharacter(65,90);
char angka = randomSeriesForThreeCharacter(48,57);
char simbol = randomSeriesForThreeCharacter(33,47);
char hurufKecil= randomSeriesForThreeCharacter(97,122);
char angkaLagi = randomSeriesForThreeCharacter(48,57);
System.out.println(hurufBesar+" "+angka+" "+simbol+" "+hurufKecil+" "+angkaLagi);
}
public static char randomSeriesForThreeCharacter(int min,int max) {
int randomNumber = ThreadLocalRandom.current().nextInt(min, max + 1);
char random_3_Char = (char) (randomNumber);
return random_3_Char;
}
}