Generating a hexadecimal string

2

Good afternoon, well, I am generating a random hexadecimal string in this pattern: 81c1328d-4dae-4af7-9974-893bb8ec90d4

But I would like to optimize this code here:

public String geraKee(){

    String letras = "abcdef0123456789";  

    Random random = new Random();  

    String armazenaChaves = "";  
    int index = -1;  

    for( int i = 0; i < 8; i++ ) {  
       index = random.nextInt( letras.length() );  
       armazenaChaves += letras.substring( index, index + 1 );  
    }
    armazenaChaves += "-";
    for( int i = 0; i < 4; i++ ) {  
       index = random.nextInt( letras.length() );  
       armazenaChaves += letras.substring( index, index + 1 );  
    }
    armazenaChaves += "-4";
    for( int i = 0; i < 3; i++ ) {  
       index = random.nextInt( letras.length() );  
       armazenaChaves += letras.substring( index, index + 1 );  
    }
    armazenaChaves += "-";
    for( int i = 0; i < 4; i++ ) {  
       index = random.nextInt( letras.length() );  
       armazenaChaves += letras.substring( index, index + 1 );  
    }
    armazenaChaves += "-";
    for( int i = 0; i < 12; i++ ) {  
       index = random.nextInt( letras.length() );  
       armazenaChaves += letras.substring( index, index + 1 );  
    }
    return armazenaChaves;  

}
    
asked by anonymous 14.03.2015 / 19:53

1 answer

6

Would not it be the case to just use% w / o%?

Example:

import java.util.UUID;

class GerarUUID {
   public static void main(String[] aArgs) {
      UUID id = UUID.randomUUID();
      System.out.println("UUID " + id);
   }
}

Output (a different value is given to each call because it is random):

UUID 7d7b34ea-7faf-4935-9829-995975751494

See working at IDEONE .

    
14.03.2015 / 19:59