a c# snippet:
//initialize the variables, experience effect is generally the same
int const experience = 1;
int success = int.MinValue();
//the chance factor is best left unhandled, also includes the time
int chance = Random.Next(1,100);
effort = 99 - chance; //reduce the effort effect based on the chance factor
//experience can at times help you, at times go against you and times doesn't help at all!
success = effort + chance + Random.Next(1,-1)*experience;
//oops we blew up somewhere..try again
//we are done yippie, time to introspect...
so basically either you finally hit the wasItWorthIt stage or you hit a stack overflow exception, good luck :-)