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 always bringing back same number

Status
Not open for further replies.

NateUNI

MIS
Jan 3, 2002
132
US
I have the following code to generate a random number. However if i leave the txtNumberSeed field the same, and run the method multiple times, it brings back the same random number. Any Help, Thanks!!!

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); }
}
 
I think it comes down to the seed your feeding the Random instance. If that seed is the same, the results will be the same. You could just try it without any seed or with System.currentTimeMillis() as seed (both will return the same result)
 
That's the whole point of using a seed. Many applications want to have a repeatable random number sequence.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top