File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Three random numbers not working

 
joseph mcgratton
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everybody,

this is my first post, although i have read this forum in the past.
i have a slight problem with a random number generator that im sure is simple to fix.

my program has 1 button and 3 text boxes. when i click the button, a random number between 1 and 5 should appear in each of the three boxes. Every time i press the button, a random number does appear in each box, but it is the same number. ie. 1 1 1 or 4 4 4. what is wrong with my code that prevents 3 random numbers being generated, as i do not want to generate a random number and repeat it three times!

here is my code.

String food;
Random f = new Random();
int randFood = (Math.abs(f.nextInt()) % 5) + 1;
food=String.valueOf(randFood);
jTextField1.setText(food);

String wood;
Random w = new Random();
int randWood = (Math.abs(w.nextInt()) % 5) + 1;
wood=String.valueOf(randWood);
jTextField2.setText(wood);

String Ore;
Random o = new Random();
int randOre = (Math.abs(o.nextInt()) % 5) + 1;
Ore=String.valueOf(randOre);
jTextField3.setText(Ore);

thankyou in advance,

joseph :-)
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you are creating a new Random object each time you push the button. This Random object is probably "seeded" with a similar value so you see the same number popping up each time. One fix is to move the Random object (f) to the class and make it a member variable. In fact, you might even want to make it static so that all instances of your class use the same random number generator.

I think this should solve your problem.

Layne
 
joseph mcgratton
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankyou for your reply.

ill use ur advice and hopefully get it to work :-)
 
joseph mcgratton
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it to work as follows

String food;
Random f = new Random();
int randFood = (Math.abs(f.nextInt()) % 5) + 1;
food=String.valueOf(randFood);
jTextField1.setText(food);

String wood;
int randWood = (Math.abs(f.nextInt()) % 5) + 1;
wood=String.valueOf(randWood);
jTextField2.setText(wood);

String Ore;
int randOre = (Math.abs(.nextInt()) % 5) + 1;
Ore=String.valueOf(randOre);
jTextField3.setText(Ore);
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I am missing something, but I don't see how that is different than the original code.

Layne
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joseph's creating a Random each time, but only one and using it three times. I'd still recommend following your full advice and storing that Random in a class, but it shouldn't be that expensive to create (it's not a SecureRandom after all).
 
joseph mcgratton
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again,

one of you did not see any difference between the origonal and fixed code so ill explain. hopefully can help people in the future.

the first code created 3 randoms - f w + o. this resulted in the 3 numbers being identical.
the second code created one random - f. this one random was used instead of w + o. the end result was three different random numbers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic