Java while loop variable jumps for initial value - java

For the following simple while loop, once in every 5 runs, I get a list of wrong values for x, as if the values jump suddenly:
package test;
public class test_old {
public static void main(String [] args) {
double r0 = 0.1;
double r = 10;
double k = 0.05;
double x = 0;
double Tt = 0;
double T = 0;
while (Tt<=30) {
double xi = Math.random();
T = Math.log(1/xi)/(k*x + r0 + r);
Tt = Tt + T;
x = x + 1;
System.out.println(x);
}
}
}
Instead of getting 1.0, 2.0, 3.0, .. etc (usually around 4 values until Tt is bigger than 30), I sometimes get a list of x values that seems to go on for ever starting at for example 89462.0, or 19945.0. The list of x values is correctly incremented by 1, but it never stops.
I am a beginner using Eclipse.
Thank you everyone for your time!

Math.random () give values 0.0<=v<1.0
so if random gives you something like
v=0.99999999, log(1/v)=0.00000000001...
continue the calculation Tt=0+0.00000000001 will take like forever to be >30
just to test replace
double xi = Math.random();
by
double xi = 0.999999999999;
and run again
I don't know why this happen but it seems that the random gives you values like 0.9999...
so to avoid this you have to insure that random gives you smaller value
you can use this post
Math.random() explained

Related

Adjust list by normalizing in java

When analysing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This adjustment can be done by normalizing to values between 0 and 1, or throwing away outliers.
For this program, adjust the values by dividing all values by the largest value. The input begins with an integer indicating the number of floating-point values that follow. Assume that the list will always contain fewer than 20 floating-point values.
Output each floating-point value with two digits after the decimal point, which can be achieved as follows:
System.out.printf("%.2f", yourValue);
Ex: If the input is:
5 30.0 50.0 10.0 100.0 65.0
the output is:
0.30 0.50 0.10 1.00 0.65
The 5 indicates that there are five floating-point values in the list, namely 30.0, 50.0, 10.0, 100.0, and 65.0. 100.0 is the largest value in the list, so each value is divided by 100.0.
For coding simplicity, follow every output value by a space, including the last one.
This is my code so far:
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double numElements;
numElements = scnr.nextDouble();
double[] userList = new double[numElements];
int i;
double maxValue;
for (i = 0; i < userList.length; ++i) {
userList[i] = scnr.nextDouble();
}
maxValue = userList[i];
for (i = 0; i < userList.length; ++i) {
if (userList[i] > maxValue) {
maxValue = userList[i];
}
}
for (i = 0; i < userList.length; ++i) {
userList[i] = userList[i] / maxValue;
System.out.print(userList[i] + " ");
System.out.printf("%.2f", userList[i]);
}
}
}
I keep getting this output.
LabProgram.java:8: error: incompatible types: possible lossy conversion from double to int
double [] userList = new double [numElements];
^
1 error
I think my variable is messed up. I read through my book and could not find help. Can someone please help me on here. Thank you so much! This has been very stressful for me.
The specific error message is because the index and size of an element must be int. So declare and assign at once: int numElements = scnr.nextInt();
Better way of programming things:
skip manual input (aka Scanner and consorts). Makes you crazy and testing a 100'000'000 times slower
you can integrate the interactive part later, once the method is done. You already know how, your code already shows.
use an explicit method to do your work. Don't throw everything into the main method. This way you can run multiple examples/tests on the method, and you have a better implementation for later.
check for invalid input INSIDE the method that you implement. Once you can rely in such a method, you can keep on using it later on.
you could even move the example numbers to its own test method, so you can run multiple test methods. You will learn about Unit Testing later on.
Example code:
public class LabProgram {
public static void main(final String[] args) {
final double[] initialValues = new double[] { 30.0, 50.0, 10.0, 100.0, 65.0 };
final double[] adjustedValues = normalizeValuesByHighest(initialValues);
System.out.println("Adjusted values:");
for (final double d : adjustedValues) {
System.out.printf("%.2f ", Double.valueOf(d));
}
// expected otuput is 0.30 0.50 0.10 1.00 0.65
System.out.println();
System.out.println("All done.");
}
static public double[] normalizeValuesByHighest(final double[] pInitialValues) {
if (pInitialValues == null) throw new IllegalArgumentException("Invalid double[] given!");
if (pInitialValues.length < 1) throw new IllegalArgumentException("double[] given contains no elements!");
// detect valid max value
double tempMaxValue = -Double.MAX_VALUE;
boolean hasValues = false;
for (final double d : pInitialValues) {
if (Double.isNaN(d)) continue;
tempMaxValue = Math.max(tempMaxValue, d);
hasValues = true;
}
if (!hasValues) throw new IllegalArgumentException("double[] given contains no valid elements, only NaNs!");
// create return array
final double maxValue = tempMaxValue; // final from here on
final double[] ret = new double[pInitialValues.length];
for (int i = 0; i < pInitialValues.length; i++) {
ret[i] = pInitialValues[i] / maxValue; // NaN will stay NaN
}
return ret;
}
}
Output:
Adjusted values:
0,30 0,50 0,10 1,00 0,65
All done.

Optimisation in Java Using Apache Commons Math

I'm trying to minimise a value in Java usingcommons-math. I've had a look at their documentation but I don't really get how to implement it.
Basically, in my code below, I have a Double which has the expected goals in a soccer match and I'd like to optimise the probability value of under 3 goals occurring in a game to 0.5.
import org.apache.commons.math3.distribution.PoissonDistribution;
public class Solver {
public static void main(String[] args) {
final Double expectedGoals = 2.9d;
final PoissonDistribution poissonGoals = new PoissonDistribution(expectedGoals);
Double probabilityUnderThreeGoals = 0d;
for (int score = 0; score < 15; score++) {
final Double probability =
poissonGoals.probability(score);
if (score < 3) {
probabilityUnderThreeGoals = probabilityUnderThreeGoals + probability;
}
}
System.out.println(probabilityUnderThreeGoals); //prints 0.44596319855718064, I want to optimise this to 0.5
}
}
The cumulative probability (<= x) of a Poisson random variable can be calculated by:
In your case, x is 2 and you want to find lambda (the mean) such that this is 0.5. You can type this into WolframAlpha and have it solve it for you. So rather than an optimisation problem, this is just a root-finding problem (though one could argue that optimisation problems are just finding roots.)
You can also do this with Apache Commons Maths, with one of the root finders.
int maximumGoals = 2;
double expectedProbability = 0.5;
UnivariateFunction f = x -> {
double sum = 0;
for (int i = 0; i <= maximumGoals; i++) {
sum += Math.pow(x, i) / CombinatoricsUtils.factorialDouble(i);
}
return sum * Math.exp(-x) - expectedProbability;
};
// the four parameters that "solve" takes are:
// the number of iterations, the function to solve, min and max of the root
// I've put some somewhat sensible values as an example. Feel free to change them
double answer = new BisectionSolver().solve(Integer.MAX_VALUE, f, 0, maximumGoals / expectedProbability);
System.out.println("Solved: " + answer);
System.out.println("Cumulative Probability: " + new PoissonDistribution(answer).cumulativeProbability(maximumGoals));
This prints:
Solved: 2.674060344696045
Cumulative Probability: 0.4999999923623868

How can I modify my if statement so that the values print out after every full second?

So basically, I have a variable, time, and would like the program to print the other values for every full second.
For example if I plug in 100, it should print out 20 seconds only.
import java.util.Scanner;
public class CannonBlaster {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
final double DELTA_T = 0.01; //initiating all variables
final double G = 9.81;
double s = 0.0;
double time = 0.0;
double second = 0;
System.out.println("What's the initial velocity?: ");//asking for the initial velocity
double v =input.nextDouble();
while (s >= 0.0) //while loop is used. As long as the height isn't negative it will continue to go.
{
s += v * DELTA_T; //demonstrates the change of velocity and position for every .01 second.
v -= G * DELTA_T;
time += DELTA_T;
System.out.println("The time is: "+time+" "+(double) Math.floor(time)+" "+Math.round(time * 1000) / 1000);
second=Math.round(time * 1) / 1;
if ((double) Math.floor(time) ==time)
{
System.out.println("Approximated position: "+ s);
System.out.println("Formula's position: "+(100.0 * time - (time*time * G) / 2.0)); //prints out the formula values and the loop values.
}
}
}
Excuse the mess, it's just I've been trying different ways to get to work, but found none so far.
The problem is that double doesn't have the kind of accuracy you're looking for, so it doesn't count by an even .01 each iteration as your output clearly shows. The solution is to use BigDecimal. I rewrote the program a bit...
package test;
import java.math.BigDecimal;
import java.util.Scanner;
public class CannonBlaster {
private static final double G = 9.81;
private static final BigDecimal DELTA_T = new BigDecimal(0.01);
private static final double DELTA_T_DOUBLE = DELTA_T.doubleValue();
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double s = 0.0;
BigDecimal time = new BigDecimal(0.0);
double time_double = 0.0;
System.out.println("What's the initial velocity?: ");// asking for the
// initial
// velocity
double v = input.nextDouble();
// As long as the height isn't negative it will continue to go.
while (s >= 0.0)
{
s += v * DELTA_T_DOUBLE;
v -= G * DELTA_T_DOUBLE;
time = time.add(DELTA_T);
time_double = time.doubleValue();
if (time.doubleValue()%1==0) {
System.out.printf("Approximated position at t=%3ds is %10.6f.\n", time.intValue(), s);
// prints out the formula values and the loop values.
System.out.println("Formula's position: " + formula(time_double));
}
}
}
private static double formula(double x){
return 100.0 * x - (x * x * G) / 2.0;
}
}
The problem is that your time step, DELTA_T, is not exactly representable as a double value. Each iteration accumulates this small error, and you can see this in the time values that get printed out.
Usually it's preferable to avoid this problem when comparing two floating point numbers by comparing the absolute difference between the two numbers to some "small" value, where "small" is defined by the problem / magnitude of numbers you are working with. DELTA_T fits pretty well here, so you could use this comparison for a per-second time step:
if (Math.abs(time - Math.round(time)) < DELTA_T)
{
// Other code here
}
Alternatively, for a more generalized time step, in PRINT_INTERVAL:
final double PRINT_INTERVAL = 0.1;
// Other code...
if (Math.abs(time / PRINT_INTERVAL - Math.round(time / PRINT_INTERVAL)) < DELTA_T)
{
// Other code here
}

generating random integers between 0 and some value where half are in the set (0,5] and the other half (5,x]

I am looking for a way to generate a random integer from 0-x, where x is defined at runtime by the human user. However, half of those numbers must be greater than zero and less than or equal to 5 (0,5] and the other half must be in the set of [6,x].
I know that the following code will generate a number from 0-x. The main problem is ensuring that half of them will be in the set of (0,5]
Math.random() * x;
I'm not looking for someone to do this for me, just looking for some hints. Thank you!
You could first flip a coin and based on that generate upper or lower number:
final Random rnd = new Random();
while (true)
System.out.println(rnd.nextBoolean()? rnd.nextInt(6) : 6 + rnd.nextInt(x-5));
Or, using the unwieldy Math.random() (bound to have trouble at the edges of the range):
while (true)
System.out.println(Math.floor(
math.random() < 0.5 ? (Math.random() * 6) : (6 + (x-5) * Math.random())
));
Consider this as a hint only :)
I'd do this:
double halfX= x / 2.0;
double random = Math.random() * x;
if( random< halfX ) {
random = random*5.0/(halfX);
} else {
random = (random/halfX - 1) * (x-5.0) + 5.0 ;
}
I think it is good now. This is less understandable and readable, but has only one call to random for each invocation. Apart from the fact MarkoTopolnic pointed out: the user needed an integer... I'd have to calculate what rounding would do to the distribution.
This is absolutely not easy... My head aches, so the best I can come up with:
double halfX= x / 2.0 + 1.0;
double random = Math.random() * (x+2.0);
int randomInt;
if( random< halfX ) {
randomInt = (int) (random*6.0/(halfX)); //truncating, means equal distribution from 0-5
} else {
randomInt = (int) ((random/halfX - 1.0) * (x-5.0) + 6.0) ; //notice x-5.0, this range before truncation is actually from 6.0 to x+1.0, after truncating it gets to [6;x], as this is integer
}
The second part I'm not sure though... A few hours of sleep would get it right... I hope the intentions and logic is clear though...
In case anyone is curious, here's the solution I came up with based on Marko's solution.
I had the following class defined for another part of this program.
public class BooleanSource
{
private double probability;
BooleanSource(double p) throws IllegalArgumentException
{
if(p < 0.0)
throw new IllegalArgumentException("Probability too small");
if(p > 1.0)
throw new IllegalArgumentException("Probability too large");
probability = p;
}
public boolean occurs()
{
return (Math.random() < probability);
}
}
With that, I did the following
private static void setNumItems(Customer c, int maxItems)
{
BooleanSource numProb = new BooleanSource(0.5);
int numItems;
if(numProb.occurs())
{
double num = (Math.random()*4)+1;
numItems = (int) Math.round(num);
}
else
{
double num = 5 + (maxItems-5)*Math.random();
numItems = (int) Math.round(num);
}
c.setNumItems(numItems);
}

Random Number Generator

I need to write a program in Java to generate random numbers within the range [0,1] using the formula:
Xi = (aXi-1 + b) mod m
assuming any fixed int values of a, b & m and X0 = 0.5 (ie i=0)
How do I go about doing this?
i tried doing this but it's obviously wrong:
int a = 25173, b = 13849, m = 32768;
double X_[i];
for (int i = 1; i<100; i++)
X_[i] = (a*(X_[i]-1) + b) % m;
double X_[0] = 0.5;
double double = new double();
System.out.println [new double];
Here are some hints:
int a, d, m, x;
Multiplication is * and mod is %.
update
Okay, I'll give you a little more of a hint. You only need one X, you don't need all these arrays; since you're only using integers you don't need any floats or doublts.
The important line of code will be
x = (a * x + b) % m ;
You don't need another x there because the x on the right hand side of the = is the OLD x, or xi-1; the one on the left side will be your "new" x, or xi.
Now, from there, you need to write the Java wrapper that will let you make that a method, which means writing a class.
Sounds like homework... so I won't give you a solution in code.
Anyways you need a linear congruential generator.
HINT: You need to write that mathematical formula as a function.
Steps:
Make a class.
Add the required state as member to the class.
Make a function within the class. Have it take input as necessary.
Write the formula for the congruential generator in Java (look up math operations in Java).
Return the result.
My Java is rusty, so I can't say I'm sure about this but these are probably errors:
int a = 25173, b = 13849, m = 32768;
double X_[i];//You need to define a constant array or use perhaps a list, you can't use i without defining it
for (int i = 1; i<100; i++)
X_[i] = (a*(X_[i]-1) + b) % m;
double X_[0] = 0.5;
double double = new double(); //You can't name a variable double, also types like double, don't need to be newed (I think)
System.out.println [new double]; //println uses () not [], in Java I think all functions need to use (), its not implied
EDIT:
Bongers:
[ ] are special symbols, if you intended for your variable to be named "X_[ i ]" that won't work. If you intended to make an array, you're making it too complicated.
You need to figure out if theY original equation was Xi - 1 or X(i-1) as that makes a huge difference in your programming. Xi - 1 is just one less than Xi. X(i-1) is the previous random number.
try doing some beginner java tutorials online. Here's a good place to start. Really try to understand the tutorials before continuing on to your problem.
Think about your problem this way.[Assuming the equation is X(i-1)] To generate the 3rd random number, X3, you will need to generate X2, which needs X1, which needs X0. But you have X0. So for any Xi, start with X0, generate X1, then generate X2, etc.. up until Xi.
You'll probably don't need to look into recursion like I first suggested.
A linear congruential generator is basically an expression which modifies a given value to produce the next value in the series. It takes the form:
xi+1 = (a.xi + b) mod m
as you've already specified (slightly differently: I was taught to always put xi+1 on the left and I still fear my math teachers 25 years later :-), where values for a, b and m are carefully chosen to give a decent range of values. Note that with the mod operator, you will always end up with a value between 0 and m-1 inclusive.
Note also that the values tend to be integral rather than floating point so if, as you request, you need a value in the range 0-0.999..., you'll need to divide the integral value by m to get that.
Having explained how it works, here's a simple Java program that implements it using values of a, b and m from your question:
public class myRnd {
// Linear congruential values for x(i+1) = (a * x(i) + b) % m.
final static int a = 25173;
final static int b = 13849;
final static int m = 32768;
// Current value for returning.
int x;
public myRnd() {
// Constructor simply sets value to half of m, equivalent to 0.5.
x = m / 2;
}
double next() {
// Calculate next value in sequence.
x = (a * x + b) % m;
// Return its 0-to-1 value.
return (double)x / m;
}
public static void main(String[] args) {
// Create a new myRnd instance.
myRnd r = new myRnd();
// Output 20 random numbers from it.
for (int i = 0; i < 20; i++) {
System.out.println (r.next());
}
}
}
And here's the output, which looks random to me anyway :-).
0.922637939453125
0.98748779296875
0.452850341796875
0.0242919921875
0.924957275390625
0.37213134765625
0.085052490234375
0.448974609375
0.460479736328125
0.07904052734375
0.109832763671875
0.2427978515625
0.372955322265625
0.82696533203125
0.620941162109375
0.37451171875
0.006134033203125
0.83465576171875
0.212127685546875
0.3128662109375
I would start by creating a class that holds a, b, m, the latest x (initialized to 0.5), and a method like getNextNumber().
public class generate_random_numbers {
public static void main(String[] args) {
int a = 25173, b = 13849, m = 32768;
Double[] X_ = new Double[100];
X_[0] = 0.5;
for (int i = 1; i < 100; i++) {
X_[i] = (a * X_[i - 1] + b) % m;
X_[i] = X_[i] / m;
System.out.println("X_[" + i + "] = " + X_[i]);
}
}
}

Categories