File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Generation of Random Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generation of Random Number" Watch "Generation of Random Number" New topic

Generation of Random Number

Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257


Can any one help me in generating random number sequence for real time application.

with following constraint,
the routine should not access the current time stamp or db time stamp.

I have tried with util.Random and math.random() they are not generated unique random numbers, after a period of time , say after every 100 or 150 numbers , they are generating again same set of numbers.

Help required.
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

unique random numbers

There is nothing in either of those classes that guarentees uniqueness. If this is a single JVM application, you might look at the UID class

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

The difficulty is in the fact that you want unique numbers.

You could do this by first making a list of all possible numbers, and then call Collections.shuffle(...) on the list to put them in random order. Then you just iterate the list to get random numbers. This solution might not be practical if the range of all possible numbers is large, or when you need a lot of random numbers.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Generation of Random Number
It's not a secret anymore!