If you are looking for a simple unique ID generator that is also (somewhat)
evenly distributed, use a reverse timestamp. Use
System.getCurrentTimeMillis(), turn it into a string, then reverse it.
If you have performance problems with such a simple algorithm, you might
want to write your own routine to do the reversing of a long without using
the generic String methods.
Jim
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]On Behalf Of
> [EMAIL PROTECTED]
> Sent: Tuesday, June 26, 2001 3:32 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [JBoss-user] Key generation by random numbers
>
>
> On Tue, Jun 26, 2001 at 11:06:42AM -0400, Richard Kasperowski wrote:
> > [EMAIL PROTECTED] wrote:
> >
> > > As a follow-up to the debate on how to get auto-increment primary
> > > keys:
> > >
> > > Is it feasible to use a random number generator to
> generate primary
> > > keys? I don't really need my records to have steadily
> increasing keys
> > > and my number of records will presumably be much smaller
> than the size
> > > of my value space (4 billion? depending on data type for
> prim-key). So
> > > if I do something along the following when making a new record;
> > >
> > > boolean created = false;
> > > do
> > > {
> > > Long key = generateRandomLong();
> > > created = ejb.create(key, contents);
> > > }
> > > while (!created);
> >
> >
> > One problem with using random numbers is that they're not
> guaranteed to
> > be unique--two calls to generateRandomLong() can return the
> same value.
>
> Yes, but that isn't really a problem because you'll just try again and
> it happens sufficiently rarely that the extra time used is
> insignificant. At least, that would be the theory.
>
> > Another problem is that computing the next random number might be
> > relatively computationally expensive. I'd say serial
> numbers are better.
>
> If that is so, it would definately be a potential problem. I don't
> know much about the effeciency of the Java random number generator
> though.
>
> Cheers
> Bent D
> --
> Bent Dalager - [EMAIL PROTECTED] - http://www.pvv.org/~bcd
> powered by emacs
>
> _______________________________________________
> JBoss-user mailing list
> [EMAIL PROTECTED]
> http://lists.sourceforge.net/lists/listinfo/jboss-user
>
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-user