random not working in .class method
Just wondering, why does Math.random() (or any random technique) not work in a constructor or setMethod? It just outputs Null. However Math.sqrt(), ect. does work inside the method.
Once I moved the Math.random() to my main class, sending the result to the setMethod, everything started to work like magic.
Note: both the class and the main have "import java.math.*;"
Re: random not working in .class method
Post the code as an SSCCE that demonstrates the error...the Math class is in the java.lang package which is a default import - there shouldn't be a need to import the java.math package.
Re: random not working in .class method
Math (Java 2 Platform SE v1.4.2)
Quote:
It just outputs Null
Math.random()'s return type is double. It cannot return null.
Always, always post code that demonstrates the issue, particularly if the problem is impossible.
Re: random not working in .class method
when I had the random in the setMethod, I had a println right after it.
pseudo - code:
Code :
public class Test()
{
public void test()
{
numberX = random() * 100
numberY = random() * 100
print ("\t" + numberX + "\t" + numberY)
}
}
outputs
however:
Code :
public main()
{
numberX = random() * 100
numberY = random() * 100
object[index].setData (numberX, numberY)
myX = object[index].getX()
myY = object[index].getY()
print ("\t" + myX + "\t" + myY)
}
outputs:
Re: random not working in .class method
Please don't post Java that won't compile. My pseudocode also occasionally prints null. Or does it? Who can tell?
Re: random not working in .class method
Only object references can have the value of null. If numberX is an int or double, it would have a printable value.
Your posted code is useless.
Re: random not working in .class method
You were asked for an SSCCE in the first response. Save us all a lot of time and just throw one together.
Re: random not working in .class method
lol i'm so far past that anyways. Moving the randoms to the main and calling a set works well. And sorry, it output "NaN" since I'm working with doubles, not Null.
Re: random not working in .class method
Here it is then.....
look for this line in folowing code:
{//resetStar method<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<here, look at next four lines
Star.java
Code java:
// star.java
// by Jon Crawford
package Star;
import java.util.*;
import java.math.*;
public class Star {
int myX;
int myY;
int myLayer;
private final int starSpeed = 1;
private final int starRadius = 1;
private final int starSteps = 0;
private final float[] invalidValues = new float[]{ -399f, -299f, 399f, 299f, 0f, 0f};
private String isValid = "false";
private double starAngle;
private double starField[] = new double[9];
public void Star(double myX, double myY, double myLayer)
{
starAngle = (Math.sqrt((myX * myX)+(myY * myY)));//calculate the hypotenuse
starField[0] = myX;//set data to star's arrary
starField[1] = myY;//this is for current position
starField[2] = myX / starAngle * starSpeed;//these set the next position
starField[3] = myY / starAngle * starSpeed;//this is reltive current position
starField[4] = myLayer;//back, mid, or foreground layer
starField[5] = starRadius;//star starting size
starField[6] = starSpeed * myLayer;//the starting speed
starField[7] = starSteps;//star has taken 0 steps, every 5 steps star grows and speeds up
starField[8] = starAngle;//store the hypotenuse
}//end constructor
public void moveStar()
{//mutator method
System.out.println("") ;
starField[0] = starField[0] + starField[2];//next position becomes the curent position
starField[1] = starField[1] + starField[3];
starField[2] = starField[0] / starField[8] * starField[6];//new next position
starField[3] = starField[1] / starField[8] * starField[6];
starField[7] = starField[7] + 1;//increment steps
if (starField[7] / 5 == (int)(starField[7] / 5) && starField[5] < 20)
{//if star has taken 5 steps
starField[5] = starField[5] + 1;//increment size
starField[6] = starField[6] + 1;//increment speed
}//end if
}//end moveStar
/* public void resetStar(double myX, double myY, double myLayer) */
/* ^^sending the values from the main works well, commented out for comparison ^^*/
public void resetStar() /* if random in here, no need to send in variables from main */
{//resetStar method<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<here, look at next four lines
myY = Math.random() * 200 - 99;
myX = Math.random() * 200 - 99;
myLayer = Math.random() * 3;
System.out.println("\t" + myX + "\t" myY + "\t" + myLayer);
/* output of this is outside of highlight tag */
starAngle = (Math.sqrt((myX * myX)+(myY * myY)));//calculate new hypotenuse
starField[0] = myX;//set data to star's array
starField[1] = myY;//same as default constructor
starField[2] = myX / starAngle * starSpeed;
starField[3] = myY / starAngle * starSpeed;
starField[4] = myLayer;
starField[5] = starRadius;
starField[6] = starSpeed * myLayer;
starField[7] = starSteps;
starField[8] = starAngle;
}//end resetStar
public void starValid()
{//isValid method
if(starField[0] > invalidValues[0] && starField[1] > invalidValues[1] && starField[0] < invalidValues[2] && starField[1] < invalidValues[3])
{//in bounds?
isValid = "true";
}else
{//out of bounds
isValid = "false";
}//end if
}//end is valid
public int getStarX()
{//get method
return (int)(Math.round(starField[0]));
}//end getStarX
public int getStarY()
{//get method
return (int)(Math.round(starField[1]));
}//end getStarY
public int getStarRadius()
{//get method
return (int)(Math.round(starField[5]));
}//end getStarRadius
public int getStarLayer()
{//get method
return (int)(Math.round(starField[4]));
}//end getStarLayer
public String getValid()
{//get method
return isValid;
}//end isValid
}//end class star
Console output using appletviewer:
NaN NaN NaN
NaN NaN NaN
this repeats indefinately.
of course to see this output properly the StarScape.java needs to be edited to remove randoms and just call resetStar without passing any variables.
these four lines should be added to the constructor as well.
properly working code posted in http://www.javaprogrammingforums.com...-day-12-a.html
Re: random not working in .class method
That is not an SSCCE - see my signature. The code you posted wouldn't even compile:
Code :
myY = Math.random() * 200 - 99;
should yield a compile time error type mismatch - myY is an int and the right side a double
Re: random not working in .class method
so I wrote up small example.java and a testRand.java subclass. Guess what? It works perfect! so something in my big starscape program messed it up, not the random function itself.
Since I crated this thread could a moderator delete this please?
Re: random not working in .class method
Quote:
Originally Posted by
Spidey1980
random does not not work in side a user made subclass method
I don't understand. Don't blame the API and the J2SE - it has worked for thousands if not millions of applications. It has been put through the ropes, and I guarantee it works if used appropriately. Almost always the problem is a result of misuse, and until you post an SSCCE that demonstrates your issue its hard to determine where the problem lies.
Re: random not working in .class method
Quote:
Originally Posted by
Spidey1980
never mind I'm not going to de-evolve my code just for a point.
Nobody wants to steal your code.
Quote:
random does not not work in side a user made subclass method
Incorrect! Random works perfectly well regardless of where you place it in your code. If it doesn't produce your expected results then either your expectations or your code is wrong.
Re: random not working in .class method
Could a problem be that this line is NOT a constructor:
Code :
public void Star(double myX, double myY, double myLayer)