Results 1 to 6 of 6

Thread: StackOverflow Error and Random function

  1. #1
    Join Date
    Mar 2010
    Posts
    182

    StackOverflow Error and Random function

    Hello,
    I have a recursive function that creates a tree. Within this function, call me a method that returns a linked list of elements chosen randomly in a greater whole. The program is repeatedly on the values of trees growing. Up to a certain size I have no problem. At one time the execution stops and I have a "StackOverflowError" has a problem with the line where the function is called random. (random.nextInt (int)). I do not have any idea about this error and if you know something about it then please let me know. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: StackOverflow Error and Random function

    Hello,
    The stackoverflow is usually from a infinite recursion. Now depending on the version of JVM used it is possible that there is a limit more or less reachable objects (it should still be pretty huge, I put instead on the infinite loop). You have to try to verify that you return your random, it is quite probable that at one time or another he will form a loop that causes the error.

  3. #3
    Join Date
    Dec 2009
    Posts
    211

    Re: StackOverflow Error and Random function

    Hello,
    I am new to java, but I have a code, I think even I had a same problem. Still check out the code and if there are some corrections to be made then correct me
    Code:
    private lstatr changemethod(int nbAtributs, int maxAttr) {
            lstatr fir = null;
            
            lstatr lstr = null;
            lstatr tpatt;
    
            Random rndm = new Random();
                int kre = rndm.nextInt(maxAttr) + 1;
                fir = new lstatr(kre);
                lstr = fir;
                
                int i = 1;
                while (i <nbAtributs) {
                    kre = rndm.nextInt(maxAttr) + 1;
                    while (existeDeja(kre, fir)) {
                        kre = rndm.nextInt(maxAttr) + 1;
                    }
                    tpatt = new lstatr(kre);
                    lstr.setChaineSuiv(tpatt);
                    lstr = lstr.getChaineSuiv();
                    
                    i + +;
                }
     
                 return fir;
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    446

    Re: StackOverflow Error and Random function

    Hello,
    Code:
    while (ext(k, first)) {
       k = random.nextInt(maxAttr) + 1;
    }
    As you generate a whole, perceive you it is already in the list (since they are all), regenerating a whole, perceive you etc. and it gives you a pretty infinite loop. Generally, such a loop and test for inserting a random value is to be avoided! Better have a list of remaining possibilities, choose a random value among them and remove it from the list of available values.

  5. #5
    Join Date
    Nov 2009
    Posts
    347

    Re: StackOverflow Error and Random function

    Hello,
    Have a look at the below code, this might be what you need.
    Code:
    Public static int fc_rc(int i) {
       if(i < 0) { return -1; / * Error * / }
       else if(i ==0 | | I == 1) { return 1; }
       else { return i * fc_rc(i-1); } / * Here we have the recursive call * /
    }
     
    Public static int fc_it(int i) {
       if(i < 0) { return -1; / * Error * / }
       int rs = 1;
       while(i> 1) {
          rs *= i;
          i -;
       }
       return rs;
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    583

    Re: StackOverflow Error and Random function

    Hello,
    See if this code may interest you.
    Code:
    Public class Hand {
     
    	static int cnt = 0;
    	
    	Public static void recrv() {
    		cnt + +;
    recrv();
    	}
    	
    	Public static void  hand(String[] args) {
    		try {
    			recrv();
    		} catch (StackOverflowError e) {
    			System.err.System.out.println("After StackOverflowError" cnt + + "Method calls ...");
    		}
    	}
    }

Similar Threads

  1. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  2. Replies: 6
    Last Post: 15-07-2011, 07:23 PM
  3. Function for two random numbers
    By Zool in forum Software Development
    Replies: 3
    Last Post: 03-12-2009, 12:39 PM
  4. Problem in a random word function
    By klite in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 12:49 PM
  5. Random Function to Concatenate 2 strings
    By klite in forum Software Development
    Replies: 3
    Last Post: 14-10-2009, 11:48 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,469,445.61446 seconds with 17 queries