Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

c++ - Why Random loss symbol does not work well

I try to make random loss from a given bit stream. Assume that I have a bit stream as

10 01 10 11 00

Now I will create a code to implement random loss. The function with two inputs are original bit stream and percent loss. Output function is output bit stream

int* bitloss(int* orbit,int size_orbit,float loss_percent)
{
srand(time(NULL));
int* out_bitstream=(int*)malloc(sizeof(int)*size_orbit);
double randval ;
for(int i=0;i<size_orbit,i++)
{
    randval = (double)rand()/(double)RAND_MAX;
    if(randval<loss_percent) 
         out_bitstream[i]=-1;
     else out_bitstream[i]=orbit[i];

}
return out_bitstream;
}

This code will change value of original bit to -1 if the random belows than loss_percent.I call -1 bit is loss bit. So given loss_percent equals 20%. That mean I will loss 2 packets from 10 original bits. But when I do it. I show that some time I loss 0 bit, some time 4 bit, and sometime 2 bit. It is not stable. How to edit my code to stable loss. For example, I want to loss 20%. So the number of -1 bits are 2. Thank you so much

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming each bit has a probability p of being lost, and that bit loss are independent (this may not be the case in for example some fading channels where bit loss are more likely to occur in bursts), the number of bit lost in N bits follows a binomial distribution.

Thus, for 10 bits and a loss rate of 20%, you would get the following distribution: B(10,0.2)

Similarly, for 1000 bits and the same loss rate of 20%, you would get the following distribution: B(1000,0.2)

Note that as the total number of bits gets larger, the binomial distribution approaches a Gaussian distribution with average Np and variance Np(1-p) . Specifically, for the case of N=1000 and p=0.2 overlapping the Gaussian distribution over the binomial distribution gives: enter image description here

As you can see it is a pretty good approximation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...