So I have the following homework, but I don't understand exactly what the process is. Has anyone seen this question before or actually understands what the logic should be? I don't want code, I kno开发者_运维技巧w how to program, but I don't exactly know what to do here.
Consider a wire across which data is transmitted bit-by-bit. Occasionally, a bit or a group of consecutive bits is transmitted incorrectly. If the previous bit was transmitted correctly, the probability that the current bit is transmitted incorrectly is 0.1. If the previous bit was transmitted incorrectly, the probability that the current bit is also transmitted incorrectly is 0.3. Write a program called BitError.java that simulates the transmission of one million bits and prints out the percentage of bits transmitted incorrectly. (Hint: According to theory, the expected answer is 12.5%.)
You test for the probability of an event happening as follows
- Generate a uniform random number between 0 and 1
- If the number generated is less than the prob an event happening then the event happend
Your code should look something like this
// Generate random bit either a 0 or a 1
int bit = RandInt(0,1)
// Assume first bit was correct
bool bPreviousWasCorrect = false;
Loop 1 million times
double probBitIsCorrect = RandUnif(0,1) // get a random number between 0 and 1
if bPreviousWasWrong then
// if an error has occured then a 2nd error occurs with prob 0.3
if (probBitIsCorrect < 0.3) then
Set bPreviousWasWrong to true
increment number of wrong bits
else
Set bPreviousWasWrong to false
increment number of correct bits
end if
else
if (probBitIsCorrect < 0.1) then
Set bPreviousWasWrong to true
increment number of wrong bits
else
Set bPreviousWasWrong to false
increment number of correct bits
end if
Display results when done
They want you to write a simulator. You make a loop which does one million iterations, each iteration representing the transmission of one bit. Every time you decide randomly if the bit gets transmitted correctly or incorrectly, based on the two rules, and keep count.
At the end, your simulation will tell you how many bits were transmitted correctly (which should apparently be close to 87.5%).
ok, so you need to "transmit" the data bit by bit and on each iteration calculate the probability.
let's consider that the transmission of the first bit is with the probability of a bit with a preceding correct transmission. this means that the first bit's probability to be transmitted correctly is 0.9
next iteration: if bit 1 was transmitted correctly, do probability 0.9 for correct, otherwise 0.7
That's why it's called homework.... It's deigned so that you don't exactly know what to do.
The problem relates to recursion and iteration. Google: Recursion. Given the current state (whether or not the previous bit was transmitted correctly or not) you can calculate the probability that the current bit is transmitted correctly. After that, it's simple probability (e.g. multiplication) to get 12.5%. You may even be able to do it without looping through all the bits, depending on how much statistics you know.
At the end you should know all about recursion. That's what the assignment is really about. What is your base case (i.e. the first bit) and what is your recursive step (i.e. each bit thereafter)? Once you understand that, writing the Java should be easy.
精彩评论