• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generation of Random Number  RSS feed

 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16046
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!