How to calculate the entropy of a file?

前端 未结 11 1338
野趣味
野趣味 2020-11-28 20:16

How to calculate the entropy of a file? (Or let\'s just say a bunch of bytes)
I have an idea, but I\'m not sure that it\'s mathematically correct.

My id

11条回答
  •  生来不讨喜
    2020-11-28 20:56

    A simpler solution: gzip the file. Use the ratio of file sizes: (size-of-gzipped)/(size-of-original) as measure of randomness (i.e. entropy).

    This method doesn't give you the exact absolute value of entropy (because gzip is not an "ideal" compressor), but it's good enough if you need to compare entropy of different sources.

提交回复
热议问题