I'm trying to generate meaningless words without using any Random() functions. I figured out that i can use current clock or mouse coordinate. And i picked to use current clock. Here is the code i wrote.
private final char[] charray = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
private char getRandomCharacter(){
return charray[random(charray.length)];
}
private int random(int value){
value =(int) System.nanoTime()% 52;
return value;
}
protected Randomizer(){
boolean running = true;
int count = 0;
int max = 5;
while(running){
StringBuilder sb = new StringBuilder();
int size = random(25) + random(25);
for (int i = 0; i < size; i++) {
sb.append(getRandomCharacter());
}
System.out.println("Random Line : " + sb.toString());
if (count++ == max) {
running = false;
System.out.println("All of them are random.");
}
}
}
public static void main(String[] args) {
new Randomizer();
}
I was expecting the out put like:
axdlMkjiIfjcmqQopv etc..
but i get something like this:
ZNNrrrUUUUxxxxbbbhhhLLLLoooRRRRRvvvYYYYBBBBfffI or JmmmPPKKKKnnnnRRBeeHHHHlllllOOOOrrrVVVVV
Why there are too much continuous. Is nanoTime
too slow for that or what?I used currentTimeMillis before that and it was much worser. I couldn't figured out and couldn't find any source about how to random with current clock.
Using randomUUID() java. util. UUID is another Java class that can be used to generate a random string. It offers a static randomUUID() method that returns a random alphanumeric string of 32 characters.
Method 1: Using UUID util. UUID class can be used to get a random string. Its static randomUUID method acts as a random alphanumeric generator and returns a String of 32 characters. If you want a string of a fixed length or shorter than 32 characters, you can use substring method of java.
To generate a random character from this string, we will use the length of setOfCharacters as the argument of random. nextInt() . Once a random integer is generated, we use it to get a character at a random index or position using charAt() .
You could get somewhat better results by defining charray
as Character[] charray
and make it a list: List<Character> chars = Arrays.asList(charray);
Use this list in getRandomCharacter()
method:
private char getRandomCharacter(){
Collections.shuffle(chars); // shuffle before each use
return chars.get(random(chars.size()));
}
And of course fix random
:
private int random(int value){
return (int) System.nanoTime()% value;
}
Output:
Random Line : tjnBUxTDeTulHfLqnEJBRBLXFqqikUYyrREzzwPwG
Random Line : MZpzJbOyCaqraRPsQPSK
Random Line : cEzKcsNHTmoVmT
Random Line : CmGXpDHGOsUufSxxStDVQruR
Random Line : XtFKmOAIisnXEdPikhAIcfzD
Random Line : GVxdnwgWLKZvQIGuofCIhiiUbKsEbmAyzVfNNPM
You can use timing data (besides other data) to seed a random number generator, but only using timing data for randomness is not easy. It is possible, but can be very slow. See for example the code I wrote here on how to use other data to seed a secure random instance (H2 database, MathUtils.generateAlternativeSeed). It uses:
This is to seed a secure pseudo-random number generator. This ensures you get enough entropy even on systems that don't have anything else running, don't know the current time, and have no UI.
But only relying on timing data, as you see yourself, is hard, as it depends on the operating system, time between method calls, the compiler, the hardware.
Using only time is problematic as it limits the frequency you can ask for a random number and is also very implementation dependent.
A better approach would be to use the time as the seed and then use a pseudorandom generator, for example a linear congruential generator. You have more info in this answer. Take into account that this random number generator algorithm is not secure, and that as Thomas pointed out using only time as a seed might not be enough if you want a secure RNG in all systems.
So your code might look like this:
private final char[] charray = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
private long seed;
private char getRandomCharacter() {
return charray[random(charray.length)];
}
private int random(int value) {
seed = (1103515245L * seed + 12345L) % 2147483648L;
return (int)seed%value;
}
protected Randomizer() {
boolean running = true;
int count = 0;
int max = 5;
seed = System.nanoTime();
while (running) {
StringBuilder sb = new StringBuilder();
int size = random(25) + random(25);
for (int i = 0; i < size; i++) {
sb.append(getRandomCharacter());
}
System.out.println("Random Line : " + sb.toString());
if (count++ == max) {
running = false;
System.out.println("All of them are random.");
}
}
}
public static void main(String[] args) {
new Randomizer();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With