Probability to find the sequence "Rar!" in a random (uniform) bytes stream at a position $\le n$ I have random (uniform distribution) stream of bytes (integers from $0$ upto $255$). What probability $p(n)$ to find the sequence "Rar!" (ASCII) at a position $\le n$ from the start of the stream?
 A: There's a simple approximate answer and a more complicated exact answer.
For the approximate answer, we can neglect the fact that the probability of finding the string at some point isn't exactly independent of the probability of having found it before that point. In this approximation, at every byte beginning with the fourth you have a $(1/256)^4$ chance of encountering your string, so the probability of having found it by $n$-th byte is approximately
$$1-\left(1-\left(\frac1{256}\right)^4\right)^{n-3}\;.$$
for $n\ge4$. (Of course it's exactly $0$ for $n\lt4$.)
To get the exact answer, you can keep track of your state according to the number of consecutive matches you've just encountered. You start in state $0$. The probability to move to state $n+1$ from state $n$ is $1/256$, and the probability to return to state $0$ is $255/256$. Once you get to state $4$ you stay there. Thus you have a Markov process with transition matrix
$$\pmatrix{\frac{255}{256}&\frac1{256}&0&0&0\\\frac{255}{256}&0&\frac{1}{256}&0&0\\\frac{255}{256}&0&0&\frac{1}{256}&0\\\frac{255}{256}&0&0&0&\frac{1}{256}\\0&0&0&0&1}\;.$$
The initial probability distribution is $1$ in state $0$ and $0$ everywhere else, i.e. $(1,0,0,0,0)$. In each step, the transition matrix is applied to this vector from the right, so after $n$ steps the distribution is the initial distribution times the $n$-th power of the transition matrix. The powers of a matrix can be expressed succinctly using its eigenvectors and eigenvalues. Wolfram|Alpha will calculate the eigensystem of the matrix for you. (I don't know how to get it to calculate the left eigenvectors, so I transposed the matrix.) Note that three eigenvalues are very small, so the corresponding components of the distribution will decay very quickly and will be negligible in practice. The initial distribution is approximately given by $\nu_1-\nu_2$, so the distribution after the $n$-th step is approximately $\nu_1-\lambda_2^n\nu_2$ (since $\lambda_1$ is exactly $1$), so the probability you want is about $1-\lambda_2^n$. If you click on "More Digits", you'll find that $\lambda_2\approx0.9999999997681$ is very close to but not exactly equal to $1-(1/256)^4\approx0.9999999997672$, so the first approximation was quite a good one.
A: Set $p=1/256^4$. Then the inclusion-exclusion principle gives the exact formula
$$p(n)=\sum_{m \geq1}(-1)^{m+1}{n-3m\choose m} p^m.$$
Only finitely many terms in the sum are non-zero, but there are about $n/4$ of them, 
so the full formula may not be too practical. On the other hand, partial sums alternate
between  upper and lower bounds on the true value $p(n)$. 
In practice, the Poisson approximation may be your best bet. 
It is easy to understand and work with:
$$p(n)\approx 1-\exp(-np).$$  
A: EDIT: As pointed out in the comments below, this answer is incorrect. However, it is a good approximation, to 16 or 17 decimal places (before exponentiation by n).

The probability of finding a given character at any position is $1/256 = 2^{-8}$, so the probability of finding "Rar!" at any position is $(2^{-8})^4 = 2^{-32}.$ 
The (unconditional) probability of finding "Rar!" at position 0 is $2^{-32}$.
The (unconditional) probability of finding "Rar!" at position 1 is $2^{-32}$.
$\vdots$
The (unconditional) probability of finding "Rar!" at position n is $2^{-32}$.
These are all unconditional (i.e., independent) probabilities. But if "Rar!" were to occur at position $i$, it can't occur at position $i+1$, $i+2$, or $i+3$, so we can't really use these unconditional probabilities, and it's actually kind of tricky to calculate directly the probability that "Rar!" will occur at least once in the first $n$ positions.
It's much easier to calculate the probability that "Rar!" won't occur in the first $n$ positions, because the act of "Rar!" not occurring does not affect the future probability of "Rar!" ocurring. The probability that it won't occur in a particular position is $(1-2^{-32})$. The probability that it won't occur in the first $n$ positions is $(1-2^{-32})^n$.
So, to answer your question, the probability that "Rar!" will occur in the first $n$ positions is
$$ p(n) = 1 - (1 - 2^{-32})^n.$$
Finally, I would just like to remark that although bytes range from 0 to 255, the ASCII encoding only ranges from 0 to 127, and a good chunk of those are control characters like "delete" or "start of header", that don't display an actual symbol/letter/digit. If it's up to you, perhaps you could choose a narrower range, if you're looking for strings in randomly generated text.
Happy number crunching!
