Skip to main content

Posts

Showing posts from January, 2011

Alphanumeric Random Number

Sometime basic things becomes tricky :-) Was trying to find out ways to generate some alphanumeric random number whose size (i.e. number of digits/alphabets) will be fixed and I should be able to decide that size. So, I came up with following algo- 1. ASCII table sequentially lists digits and alphabets e.g. ASCII code of 0 to 9 are ranging from 48 to 57. So I can use this sequential ASCII coding and convert them to Java Characters. Benefit, of course I can get rid of any array of predefined characters in my code. 2. I want to add current time to this "randomness", so need to initialize the seed of Java Random object with current timestamp. So every time I execute this, I can make sure it will be something different than previous instance. 3. As I already explained, it will be alphanumeric based, so I'll exclude special characters and thus, total number of valid ASCII chars left for me is 62 ( 0-9, A-Z, a-z ) . Now, let's see how the Java code looks like. Ple