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!
I would start reading up Pseudorandomness and then some common Pseudo-random number generators. Of course, your language hopefully already has a suitable "random" function :-)
Using some simple command line (bash scripting):
$ 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
n
chars'\n'
charWhy 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:
http://kickjava.com/src/org/apache/commons/lang/RandomStringUtils.java.htm
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);
}
}
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
The easiest is to do the following:
String alphabet
with the chars that you want.N = alphabet.length()
java.util.Random
for an int x = nextInt(N)
alphabet.charAt(x)
is a random char from the alphabetHere'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)));
}
See below link : 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.
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,
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.
random char package 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;
}
}
Source: Stackoverflow.com