Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help getting user input into methods

OKay I have this program, The problem is I don't know what the idea of the seed value is? I have a rollDice Method and I want to Simulate the rolling of two dice and then add them together and then print out a result. The result is the result of the two random dice being rolled and then added together giving me a random number. Now what I want to do is with the GetMean Method, I want to Check to see and Find the mean, That is However many times the method RollDice was called I want to store that number into the index value in getMean, I then want to divide the rollDice number by the index? Is this possible? I'm just really stuck here, ANy Helps, Hints, Tips appreciated, Thank You!

import java.util.Random;
import java.util.*;

public class Histogram
{
private Random randomGenerator;

/**
* The Histogram constructor gets a seed value for the
* random number generator.
*
* The constructor needs to create a HistogramUI object. You
* also need an array to keep track of the dice roll values.
*/
public Histogram( long seed )
{
randomGenerator = new Random(seed);
int r = randomGenerator.nextInt();
}

public int rollDice()
{
int result;
int roll = randomGenerator.nextInt(6) + 1;
int roll2 = randomGenerator.nextInt(6) + 1;
return result = roll + roll2;
}

public double getMean()
{
int total = 0;
for(int index = 0; index < rollDice(); index ++){
total = rollDice() / index;
// total = rollDice();
}
return total;
}
//public double getMedian()
//{
//}

/**
* This method needs to call the HistogramUI methods to set up and
* display a Histogram.
*/
public void displayHistogram()
{
HistogramUI histogram = new HistogramUI( "Dice" );
histogram.setMax(rollDice());

histogram.setRollCount(2, 12);

histogram.setMean(getMean());

histogram.setMedian(3.2);

histogram.setMode("23");

histogram.setVisible();
}


}
[2428 byte] By [RPBLEA] at [2007-11-11 7:05:53]
# 1 Re: Help getting user input into methods
I have adjusted the code a bit. The way you were calculating the mean
would yield unprecise results. You should do the mean calculation after
you have accumulated the total, and then divide by the count.

import java.util.Random;
import java.util.*;
import java.text.*;

public class Histogram {
private Random randomGenerator;
private static DecimalFormat dF=new DecimalFormat("0.000");
/**
* The Histogram constructor gets a seed value for the
* random number generator.
*
* The constructor needs to create a HistogramUI object. You
* also need an array to keep track of the dice roll values.
*/
public Histogram(long seed) {
randomGenerator = new Random(seed);
}

private int rollDice() {
int result;
int roll = randomGenerator.nextInt(6) + 1;
int roll2 = randomGenerator.nextInt(6) + 1;
return result = roll + roll2;
}
public double getMean() {
int total = 0;
int rolls=rollDice();
for (int i=0; i < rolls; i++) {
total += rollDice();
}
return (double)total/(double)rolls;
}
public static void main(String[] args) {
Histogram h=new Histogram(System.currentTimeMillis());
double mean=h.getMean();
System.out.println("Mean: "+dF.format(mean));
}
//public double getMedian()
//{
//}

/**
* This method needs to call the HistogramUI methods to set up and
* display a Histogram.
*/
public void displayHistogram() {
HistogramUI histogram = new HistogramUI("Dice");
histogram.setMax(rollDice());

histogram.setRollCount(2, 12);

histogram.setMean(getMean());

histogram.setMedian(3.2);

histogram.setMode("23");

histogram.setVisible();

}

}
sjalle at 2007-11-11 22:39:22 >