lundi 10 mai 2021

Sampling packets

I am programming a hardware packet processor, and I am trying to sample packets. My goal is to keep one out of 10 packets. However, I am aware that one cannot just keep every 10th packet as that would not be a correct sampling method.

I use a random number generator, although the number is always between 0 and (2^n) - 1. That is, if the random number is 4 bits in size, the random number generator will generate a number between 0 and 15.

My first approach was to generate a number of size 10 or 11 bits. Say a 10-bit number n would go between 0 and 1023. I was planning to modulo 10 the n random number and pick one out of the 10 numbers. Unfortunately, this is not possible as my hardware packet processor does not contemplate modulo operations without knowing n at compile time.

My second option was to make a simple if:

// between 0 and 1023. random() retrieves a random number with uniform distribution
int<10> n = random();

if n < 102{
  keep_packet()
} else{
  drop_packet()
}

I wonder if this last method is indeed a correct sampling method and as correct as doing n modulo 10.

Thanks




Aucun commentaire:

Enregistrer un commentaire