3

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.

6
  • 1
    so you mean abc123, or it can also be a1b2c3 ? Commented Mar 19, 2010 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. Commented Mar 19, 2010 at 15:18
  • Please provide sample output. What strings should be generated, and what strings should not be generated? Commented Mar 19, 2010 at 15:31
  • sample output are AFZ391, EBD391, GBH491 ... :) use random method :) Commented Mar 19, 2010 at 15:36
  • sounds like homework, retagged. Commented Mar 19, 2010 at 18:37

10 Answers 10

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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?

Comments

1

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)); } 

1 Comment

To be honest this looks more complicated than a good solution without your library. BTW Java already comes with assert.
0

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

Comments

0

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.

Comments

0

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.

Comments

0

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; 

Comments

0

You could have a look at RandomStringUtils, or at least at its source code.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.