?
Solved

Randomness failed.

Posted on 2003-03-06
6
Medium Priority
?
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

764 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