Generate 6 characters: the first character is randomly generated from the alphabets with odd ordering in the alphabet list (A, C, E, …, Y) the second character is randomly generated from the alphabets with even ordering in the alphabet list (B, D, F, …, Z) the third character is randomly generated from alphabet list (A to Z) each of the three digits is random generated from 1 to 9.
-
1so you mean `abc123`, or it can also be `a1b2c3` ? – Bozho Mar 19 '10 at 15:02
-
Here is the full questions. Generate 6 characters: the first character is randomly generated from the alphabets with odd ordering in the alphabet list (A, C, E, …, Y) the second character is randomly generated from the alphabets with even ordering in the alphabet list (B, D, F, …, Z) the third character is randomly generated from alphabet list (A to Z) each of the three digits is random generated from 1 to 9. – LC. Mar 19 '10 at 15:18
-
Please provide sample output. What strings should be generated, and what strings should not be generated? – fredoverflow Mar 19 '10 at 15:31
-
sample output are AFZ391, EBD391, GBH491 ... :) use random method :) – LC. Mar 19 '10 at 15:36
-
sounds like homework, retagged. – Valentin Rocher Mar 19 '10 at 18:37
-
http://www.texamples.com/how-to-generate-random-passwords-in-java/ this might help you – brainless Aug 28 '12 at 18:16
10 Answers
Is this homework? If so please tag your question appropriately.
Here is a clue: letters and numbers are all characters, which you could store in an array.

- 13,822
- 6
- 44
- 64
In java you can do char arithmetics. So
'A' + RNG.nextInt(26);
will return you a random letter between 'A' and 'Z', where RNG
is an instance of java.util.Random
.
To build the string efficiently. Use a StringBuilder

- 40,153
- 18
- 88
- 147
using my library dollar is simple:
@Test
public void generateRandomString() {
String string = $('a', 'z').shuffle().slice(3).join() + // take 3 random letters
$('0', '9').shuffle().slice(3).join(); // take 3 random digits
assertThat(string.length(), is(6));
}

- 114,442
- 31
- 189
- 228
-
1To be honest this looks more complicated than a good solution without your library. BTW Java already comes with `assert`. – Cam Mar 19 '10 at 15:45
Not sure if this is homework (it looks like it is), so I'll try to point you in the right direction of a possible approach:
- Recall that a random integer can be any integer X between two other specified integers Y and Z.
- How can you go from a random number to a random CHARacter?
- How could you take a random number between 0 and 13, and turn that into an even number between 0 and 26? An odd number?
- How can you use these ideas/concepts to your advantage for answering this question?

- 14,930
- 16
- 77
- 128
Try with xeger and brics automaton.
import nl.flotsam.xeger.Xeger;
import dk.brics.automaton.Automaton;
public class RandomizeString{
public String generateRandomString(){
String regex = "[ACEGIKMOQSUWY][BDFHJLNPRTVXZ][A-Z][0-9]{3}";
Xeger generator = new Xeger(regex);
String result = generator.generate();
return result;
}
}
to understand more, learn Regular Expression.

- 5,698
- 16
- 59
- 82
use the random generator function to generate a number in the range [0,26) and add the value of (int)'a' to that, and cast the result back to a char

- 936
- 7
- 12
Generate a set of numbers between 0 - 61 (there are 61 letters for upper and lower, plus digits) and map each to one of [0-9a-zA-Z], then concatenate the whole thing together.

- 14,762
- 8
- 58
- 80
Some basic things you can use:
- an array of all 26 characters in the alphabet and,
- 1 or 2 instances of a random number generator.

- 5,573
- 7
- 33
- 38
I want to generate 6 random characters including 3 random letters followed by 3 random numbers but I can only generate only letters or numbers at one time.
char a = randomLetter();
char b = randomLetter();
char c = randomLetter();
int x = randomNumber();
int y = randomNumber();
int z = randomNumber();
String result = new String()+a+b+c+x+y+z;

- 256,549
- 94
- 388
- 662
You could have a look at RandomStringUtils, or at least at its source code.

- 11,667
- 45
- 59