Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Randomness failed.

Posted on 2003-03-06
6
Medium Priority
?
191 Views
Last Modified: 2010-03-31
Hello All,

I am using the following to create some random numbers.

java.util.Date date = new java.util.Date();
          java.text.SimpleDateFormat sdf= new java.text.SimpleDateFormat("yyyyMMddhhmmssSS");
          String sdate = sdf.format(date);
          order_no="DIM"+sdate;

But, sometimes the process is too fast..that it gives me duplicated values.

How can i add some randomness to the same code i have mentioned.

Thanks and regards
0
Comment
Question by:newtosql
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8081762
There is nothing in the code you posted that indicates random generation. Did you post the wrong code?
0
 
LVL 1

Expert Comment

by:shji1
ID: 8081815
I'm not so sure what are you trying to de here because it's not about randoms. If you really want to deal with random numbers, why not using the Random class.
But if you "have to" use the Date... you can save the last date created, and when you create the next date just check if they are equals. If they are, create another date until they are not equals.
You can easily do that in a do-while block.
0
 

Author Comment

by:newtosql
ID: 8081831
Sorry for not being specific. Actually the code is in a method called getOrder(): like the below

     public static synchronized String getOrder()
     {
     String order_no=null;
     try
          {
          java.util.Date date = new java.util.Date();
          java.text.SimpleDateFormat sdf= new java.text.SimpleDateFormat("yyyyMMddhhmmssSS");
          String sdate = sdf.format(date);
          order_no="DIM"+sdate;
          }
     catch(Exception e)
          {e.printStackTrace();}
     return(order_no);
     }
So, each time i called this method...i expect a different number. Am i specific now??
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Accepted Solution

by:
TimYates earned 120 total points
ID: 8081841
public static synchronized String getOrder()
{
  String ret = "" + System.currentTimeMillis() ;
  try{ Thread.sleep( 1 ) ; } catch( InterruptedException ex ) { }
  return ret ;
}
0
 

Author Comment

by:newtosql
ID: 8082059
Hello Tim,

It works fine. Thanks for your help.
Keep up the good work

regards
newtosql
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8082429
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question