If so how, and what value would this rating have in understanding the code?
For example, I would guess that programs using genetic or neutral algorithms would be less predictable than one calculating the sum of pi.
You seem to be asking a variation of the halting problem, so no, there is not a general solution to determine how predictable a program is.
However, you can throw a battery of tests and code reviews at any application and get a heuristic confidence to a program's predictability.
If you write units tests and pass the tests with colors, you are pretty safe saying it will run in a predictable fashion.
Of course nothing is 100%. Mechanical failures happen, account for those as well. A client machine can have it's power supply explode due to someone spilling coffee in it. Is your server prepared to handle the case of suddenly losing contact with a client?
It's all just a matter of putting safe guard after safe guard after safe guard.
To predict, you have to understand and that is based on your ability relative to the complexity of the application. The rating of complexity can be based on the level of the developer who could understand it: beginner, medium, expert.
Cyclomatic complexity might help you understand how predictable a program is.
As you and others have implied, there seems to be a correlation (if not a causation) between complexity and predictability.
If the system (your program is part of) is not Chaotic it will be pretty predictable.
If the system is chaotic... may be very hard to predict.
Now all you have to do is convert measures of the code like cyclomatic complexity ( which we can measure) into the sort of maths used in complexity theory.
Don't forget to model the OS and runtime as well.
rand()
stack up on your predictability meter? The result of calling it is unpredictable, but that is the point of the function; it is predictably unpredictable (if that makes sense). ie, are you looking for predictability in the sense that doingx
will always result iny
, or are you looking for a way to determine if given an inputx
whether it will always do 'the right thing'?rand
is equally so. A GA gets its unpredictability from a good PRNG used to form the initial population and during crossover and mutation.πr²
, the square is4r²
. The ratio of the darts that hit the circle to to all darts isπ/4
. (This was the first ever randomized algorithm I learned and the one which was used to introduce us to the concept of randomized algorithms in general and Monte Carlo algorithms in particular It might have even been all the way back to high school.) You could also use Buffon's Needle in the same way.