Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: random not working in .class method

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default 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.*;"


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default 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.

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: random not working in .class method

    Math (Java 2 Platform SE v1.4.2)
    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.

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: random not working in .class method

    when I had the random in the setMethod, I had a println right after it.

    pseudo - code:
    public class Test()
    {
    public void test()
    {
    numberX = random() * 100
    numberY = random() * 100
    print ("\t" + numberX + "\t" + numberY) 
    }
    }

    outputs
    null       null

    however:
    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:
    47       82
    Last edited by Spidey1980; August 18th, 2011 at 01:41 PM.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default 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.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,256
    My Mood
    Hungover
    Thanks
    142
    Thanked 618 Times in 529 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default 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.

  9. #9
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default 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
    // 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 It works!!! eureka!!! Java...Day 12.
    Last edited by Spidey1980; August 18th, 2011 at 02:27 PM.

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: random not working in .class method

    That is not an SSCCE - see my signature. The code you posted wouldn't even compile:
    myY = Math.random() * 200 - 99;
    should yield a compile time error type mismatch - myY is an int and the right side a double

  11. #11
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default 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?
    Last edited by Spidey1980; August 18th, 2011 at 10:49 PM.

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: random not working in .class method

    Quote Originally Posted by Spidey1980 View Post
    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.

  13. #13
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: random not working in .class method

    Quote Originally Posted by Spidey1980 View Post
    never mind I'm not going to de-evolve my code just for a point.
    Nobody wants to steal your code.
    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.
    Improving the world one idiot at a time!

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: random not working in .class method

    Could a problem be that this line is NOT a constructor:
     public void Star(double myX, double myY, double myLayer)

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 04:46 PM
  2. HELP random generator with roll() and print() method
    By disc_dido in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 18th, 2011, 01:50 PM
  3. method not working outside of class
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2011, 10:00 AM
  4. Using Random class
    By javapenguin in forum What's Wrong With My Code?
    Replies: 22
    Last Post: November 18th, 2010, 01:34 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM