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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…