ID generator

import java.util.Random; class Id { private int longitud; private int decoder; private char[] idArray; public Id(int longitud){ this.longitud = longitud; } public void generarId(int longitud){ boolean flagIntOrString = true; idArray = new char[longitud]; for(int i=0;i<longitud;i++) { Random num = new Random(); if (num.nextDouble() > 0.5) { int c = (int) ((num.nextDouble() * 95) + 97); char cc = (char) c; idArray[i] = cc; }else{ char c = (char) ((num.nextDouble() * 57)-9); idArray[i] = c; } } }
It's very simple, is a id generator. A number combined with characters array. Mostly used for keys and unique IDs.

2 Responses

It's a very cool homemade id gen :). In java you can use UUID either https://docs.oracle.com/javase/7/docs/api/java/util/UUID.html if the project permits.
Just a little ideia in code pattern matter, I'd just change the constructor to private e make generateId a static factory (Bloch, Effective Java).
i.e.
public static Id generateId(int logitud){
Id id = new Id(longitud);
id.idArray = new String[logintud];
....
return id;
}
this helps to build more semantic sentences :D i.e. Id.generateId(10);
Just an ideia (I like to share and discuss code pattern stuff hehehe):D
Best regards!
Hi Kim :). Thanks, now it's updated and tested. And, yes, this code your help to make semantic sentences. I'm using for create UNIQUE id for database with 500 registered persons. Regards!

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.