Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



StackOverflow Error and Random function

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 11-03-2010
Member
 
Join Date: Mar 2010
Posts: 183
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.
__________________
www.techarena.in
Reply With Quote
  #2  
Old 11-03-2010
Member
 
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.
Reply With Quote
  #3  
Old 11-03-2010
Member
 
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;
}
Reply With Quote
  #4  
Old 11-03-2010
Member
 
Join Date: Nov 2009
Posts: 447
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.
Reply With Quote
  #5  
Old 11-03-2010
Member
 
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;
}
Reply With Quote
  #6  
Old 11-03-2010
Member
 
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 ...");
		}
	}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "StackOverflow Error and Random function"
Thread Thread Starter Forum Replies Last Post
Generate some quality of random number via Random Number Generation function aMADeO! Windows Software 4 4 Weeks Ago 05:07 PM
Internet Script error box pops up and list a random website in the error box Jagathi Networking & Security 6 15-07-2011 08:23 PM
Function for two random numbers Zool Software Development 3 03-12-2009 12:39 PM
Problem in a random word function klite Software Development 3 30-11-2009 12:49 PM
Random Function to Concatenate 2 strings klite Software Development 3 14-10-2009 12:48 PM


All times are GMT +5.5. The time now is 03:46 AM.