Link to home
Create AccountLog in
Avatar of dlcnet
dlcnetFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Number generation

Hi Experts!

 I would like to generate a particular 10 digit number. The first 4 digits are related to userID and the rest 6 should be "random" or ordered but they must be synchronized since they are used as an unique identifiers. The ideea is to split the code in two. One who generates the 6 digits and later on I can add the 4 digits. How one can do that?

Thanks!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to keep a record of numbers generated. You could use a Set, serialized if necessary
int first = ( int )( Math.random() * 1000000 ) ;
int second = ( int )( Math.random() * 10000 ) ;

String final = first + "" + second ;
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Long time;
String partNumID;
time = new Long(System.currentTimeMillis());

partNumID = new Integer(Math.abs(time.hashCode())).toString().substring(0,6);
or rather :

Long time;
String partNumID;
StringBuffer reverse;

time = new Long(System.currentTimeMillis());
try{
Thread.sleep(100);
}catch(InterruptedException intex){}

reverse = new StringBuffer(new Integer(Math.abs(time.hashCode())).toString()).reverse();
partNumID = reverse.substring(0,6);
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I never did explain my code ...

It takes the hashcode of a string of the current system time in milliseconds, and reverses it (the extra milliseconds being evident at the right-hand end of the system time), using the first 6 of those positions as the unique result. If you put that code in a loop, you could generate about a quarter million values an hour. You can then iterate those values, prepending your UID complexion.