1

please can I ask you because I can't remember :

I want to create a random data of clients with random initials and random number of id's

I have a String s = "ABCD..XYZ"; How do I get the letters into an array (this is the difficult part) and then randomly pick elements and create my data. Thanks.

MByD
  • 135,866
  • 28
  • 264
  • 277

5 Answers5

2

Getting letters into the array is quite simple:

    String s = "ABCD..XYZ";
    char[] characters = s.toCharArray();

From then on just pick random characters:

    int random = rand.nextInt(s.length());
    System.err.println(characters[random]);
planetjones
  • 12,469
  • 5
  • 50
  • 51
0

(this is a duplication of an answer I just gave someone else, but thought it may fit here)

Stelyian,

I have a very simple class that may help with "semi-random" data. It's extremely easy to use and will give you different types of strings for your data.

Maybe it will help. Other answers around here will give you true fuzz data if that's what you're looking for. Good luck!

PhraseGenerator

http://metal-sole.com/2012/10/12/random-phrases-computers-is-funny/

bladnman
  • 2,591
  • 1
  • 25
  • 20
0

If you just want to pick a random char from your string, do something like this:

String s = "ABCDEFG...Z";
Random r = new Random(System.currentTimeMillis());

s.charAt(r.nextInt(s.length())); // THIS WILL GET A RANDOM CHAR FROM YOUR STRING

If you need to create random names or something, just iterate this several times adding these chars to your string to create your random names.

Tiago Pasqualini
  • 821
  • 5
  • 12
  • Thanks, I need to add them to an array. I guess, my point is to get a list of customers which has some properties, etc. to perform some tasks for me . Thanks! – Stelyian Stoyanov Mar 20 '12 at 13:23
0

This creates a random ten letter word:

public static void main(String[] args) {
    char[] letters = new char[10];
    for (int i = 0; i < 10; i++) {
        letters[i] = (char) ('A' + new Random().nextInt(26));
    }
    String s = String.copyValueOf(letters);
    System.out.println(s);
}
assylias
  • 321,522
  • 82
  • 660
  • 783
0

Use StringBuilder + Random.nextInt().

StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 10; i++) {
    builder.append((char) ('A' + random.nextInt(26)));
}
System.out.println(builder.toString());
Adam
  • 35,919
  • 9
  • 100
  • 137
  • Could be OK, but I'd use a mixture of upper/lower case, digits, and extra characters. – Adam Mar 20 '12 at 13:30