Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Class

Status
Not open for further replies.

Uray

Programmer
Oct 20, 2003
2
US
I am believing I have seen answer before but can't find. The code I have from before is:
public void seedRandomGenerator(){
try{
long seedNumber = Long.parseLong(txtNumberSeed.getText());
Random random = new Random(seedNumber);
randomNumber = 1 + random.nextInt(nameCounter-1);
System.out.print(randomNumber);
JOptionPane.showMessageDialog(null,"New Seed Value in use","Error",JOptionPane.INFORMATION_MESSAGE); txtNumberSeed.setText("");
}
catch(IllegalArgumentException illegalArgumentException){
JOptionPane.showMessageDialog(null,"Please input a Random Seed Value","Error",JOptionPane.INFORMATION_MESSAGE); }
}

But same number comes back is problem.
Thanks
Uray
 
Uray,
I remember commenting on this type of situation against Oracle 8i a couple of months ago? Or maybe more recently. Anyway the issue is "generally" the same in Java/Oracle comment from before.

It looks like you are looking for a repeatable number sequence in your application? If so the basis is the "seed" you are using in the random sequence. You can't get different results with the same seed.

Your code looks OK but try it in your app withour any seed. I know that System.currentTimeMillis() will come back with the same result, whether its right or not for app remains to be seen.

Rabbi
 
Rabbi,
I am thanking you, but asking too if the sequence is for sure random after?

Uray
 
Well a pretty much textbook answer would be that the easiest way to initialize a random number generator is to use the parameterless constructor, for example

Random generator = new Random();

Beware of one thing when you use this constructor: Algorithmic random number generators are not truly random, they are really algorithms that generate a fixed but random-looking sequence of numbers. When you create a random number generator, it initializes its sequence from a value called its "seed". The parameterless constructor for Random uses the current time as a seed, which is usually as good a seed as any other. However, the time is only measured to a resolution of 1 millisecond, so if you create two random number generators within one millisecond of each other, they will both generate exactly the same sequence of numbers.

That was why I suggested using your own seed and being careful what you use. You can use any long integer as a seed. There are no magic formulas for picking a good seed.

Rabbi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top