Program 1: TCP bandwidth

Suppose congestion is such that packets are lost with probability p, eg p=0.01 means 99% of the packets get through. Let us model this in TCP by assuming that if a packet gets through successfully,
    cwnd += 1/cwnd
but if a packet is lost,
    cwnd = cwnd/2
We will ignore any delay resulting from a lost packet; it's not actually relevant to the problem here but we can assume that some abstract form of instantaneous lost-packet notification takes place. In particular, slow-start is not being used. We are also assuming that the loss probability p is not influenced by cwnd. In real life, as cwnd grows then p would increase, unless there were very many connections. Which, in many cases, there are. ;-)

The question is what is the steady-state average for cwnd?

Write a program to find out, through simulation.

Using Visual C++, you can get a random number in the range 0..1 with
    double(rand())/RAND_MAX
If this quantity is less than p, then set cwnd=cwnd/2 (minimum 1!); otherwise set cwnd+=1/cwnd. (Note cwnd must be of type double, initially 1.0.) Run this for "a long time", and see what steady-state behavior you get. (I used 100,000 runs to get the average; you can probably get away with a smaller number.)

Do this for a variety of different values of p, and try to find a relationship. Hint: as p gets smaller, the limiting cwnd gets larger; you might try plotting 1/p versus cwnd.

Note that cwnd will continue to oscillate in an irregular sawtooth; what you are trying to measure is the average value. It is very important that the data you collect represents long-term averages of cwnd.

If you use a 15-bit random-number generator, then you can probably only get p down to about 0.002 before roundoff issues overwhelm things. Try a random-number generator with more bits, or concatenate two 15-bit random numbers to get a 30-bit number, or just try to figure the relationship based on data using p=0.1 down to p=0.002.

In trying to establish the relationship, it is also very important that you don't compress one of the axes, without care. If you want to use a logarithmic axis for p (or 1/p), that works, but you must then also use a logarithmic axis for cwnd!. Otherwise just stick with linear axes (but plot 1/p).