# Random number with a total.

• 06-11-2013, 10:47 PM
MR bruto
Random number with a total.
hey guys.

im making this dicing simulation program.
what it does: it generates a random number when you click the button "hit".
it's in a GUI.

whenever you click the button it creates a random number and displays it, and next to it it displays the total(ill explain later).

the total is: every number generated, added up.

like if you click the button 2 times:
it generates these numbers(example): 14, 86
then i want the total to be: 100.

if you click the button 3 times:
it generates these numbers(example):30, 70, 80
then i want the total to be: 180.

with the current code i have, the total is always equal to the random number generated. (which i dont want..).

i don't really know how to do it.
i tried thinking about a for-loop but i dont know how to put it in that for loop..

so if anyone could help me with this, would be cool.

code:
Code:

```final JButton btnRoll = new JButton("Hit");                 btnRoll.addActionListener(new ActionListener() {                         public void actionPerformed(ActionEvent e) {                                 Random rand = new Random();                                 int rand1 = rand.nextInt(100)+1;                                                                 lblNewLabel.setText(""+rand1);                                                                 int totrand = +rand1;                                                                 lblNewLabel_1.setText("Total: "+totrand);                                                                                                                                                                                         }                 });```
• 06-11-2013, 10:55 PM
bigsonny
Re: Random number with a total.
It can be more complicated but to keep it simple, try

changing

Code:

` int totrand = +rand1;`
to
Code:

` int totrand = totrand + rand1;`
I suppose you could also use a loop but I am not sure if that's necessary since you have an ActionListener object...
• 06-11-2013, 11:01 PM
superhaNds
Re: Random number with a total.
Why would you need a for loop? just as bigsonny said, totrand += rand1;
so each time the button is pressed, the random numbers are added to the sum, which you store in totr
• 06-12-2013, 09:22 AM
MR bruto
Re: Random number with a total.
Quote:

Originally Posted by superhaNds
Why would you need a for loop? just as bigsonny said, totrand += rand1;
so each time the button is pressed, the random numbers are added to the sum, which you store in totr

well both of your suggestions didnt work.

everytime i click the button, it generates a random number and displays that random number in the total.
but when i click the button again, it does exactly the same.
it doesnt add those 2 numbers.

i cant make it work
• 06-12-2013, 10:12 AM
JosAH
Re: Random number with a total.
The 'totrand' variable is local to the method in the ActionListener; you get a new one each time that method is called.

Jos
• 06-12-2013, 12:52 PM
superhaNds
Re: Random number with a total.
yeah sorry, totrand should be declared in class scope.