Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 15th, 2005, 10:18 AM   #1
Hadrurus
Newbie
 
Hadrurus's Avatar
 
Join Date: Aug 2005
Location: Northampton, England
Posts: 8
Rep Power: 0 Hadrurus is on a distinguished road
Send a message via MSN to Hadrurus
Question Random Number & Average Problem

I wonder if I could find any help with a problem I have regarding my latest program...

Im trying to generate a number at random between 200 & 400 and also display the average everytime a button is clicked, so the average will rise and fall but in theory eventually converge on or around 300.

So far my program only generates a number between 0 & 400 (not 200 & 400 as I need it to) and I havent any ideas for how to show an average after every click

Any help please?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class RandomNumber extends JFrame implements ActionListener {

    private JButton button;
    private JTextField valuesTextField, resultTextField;
    private Random random;

    public static void main(String[] args) {
        RandomNumber frame = new RandomNumber();
        frame.setSize(200,150);
        frame.createGUI();
        frame.show();
    }

    private void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout());

        button = new JButton("Generate");
        window.add(button);
        button.addActionListener(this);

        valuesTextField = new JTextField(14);
        window.add(valuesTextField);

        random = new Random();
    }

    public void actionPerformed(ActionEvent event) {
     
    
        int one; 

        one = random.nextInt(400) + 1; 
        
        
        valuesTextField.setText("Number: "
            + Integer.toString(one));  }

}
__________________
-Hadrurus
Even if the voices aren't real, they have some good ideas.
Hadrurus is offline   Reply With Quote
Old Aug 15th, 2005, 10:26 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
Well, you're asking it for a number between a number between 1 and 400, so that's what it's going to give you. To get a number between a and b, you'll want to do this:
random_number = random.nextInt(b - a) + a + 1;
See if you can figure out why that's the case.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 15th, 2005, 10:27 AM   #3
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
Use this for the random number:
one = 200 + random.nextInt(200);
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2

Last edited by skuinders; Aug 15th, 2005 at 10:38 AM.
skuinders is offline   Reply With Quote
Old Aug 15th, 2005, 10:37 AM   #4
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
Here's a simple implementation of what you want:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class RandomNumber extends JFrame implements ActionListener {

	private JButton button;
	private JTextField valuesTextField, resultTextField, meanTextField;
	private Random random;
	private int total = 0;
	private int count = 0;
	
	public static void main(String[] args) {
		RandomNumber frame = new RandomNumber();
		frame.setSize(200,150);
		frame.createGUI();
		frame.show();
	}

	private void createGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container window = getContentPane();
		window.setLayout(new FlowLayout());

		button = new JButton("Generate");
		window.add(button);
		button.addActionListener(this);

		valuesTextField = new JTextField(14);
		window.add(valuesTextField);

		meanTextField = new JTextField(14);
		window.add(meanTextField);

		random = new Random();
	}

	public void actionPerformed(ActionEvent event) {
		int r;
		count+=1;
		r = 200 + random.nextInt(200); 
		total+=r;
		valuesTextField.setText("Number: " + Integer.toString(r));  
		meanTextField.setText("Mean: " + Double.toString((double)total/count));  
	}
}
You just have to add variables that will keep track of the total and count and add a text box to display the mean.
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Old Aug 15th, 2005, 10:38 AM   #5
Hadrurus
Newbie
 
Hadrurus's Avatar
 
Join Date: Aug 2005
Location: Northampton, England
Posts: 8
Rep Power: 0 Hadrurus is on a distinguished road
Send a message via MSN to Hadrurus
Thanks Ooble and Skuinders both your help much appreciated The program works perfectly now.

thanks again,
__________________
-Hadrurus
Even if the voices aren't real, they have some good ideas.
Hadrurus is offline   Reply With Quote
Old Aug 15th, 2005, 12:50 PM   #6
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
Speaking of a random number, how would you get a random number in C? I'm curious about manipulating the range of the random number too.
Navid is offline   Reply With Quote
Old Aug 15th, 2005, 1:08 PM   #7
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
http://cplus.about.com/od/cprogrammi.../aa041403c.htm
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC