educative.io

Educative

Generate number between 0 and 100 with md5

Hello,
For database scaling and the consistent hashing, they used md5 to get a number from 1 to 100 (to find the correct partition for the data)

md5 produces a value like : 5D41402ABC4B2A76B9719D911017C592
How from there do you get a number between 1 and 100 ?

Thank you

Hi @Santrhyl,

The md5 hash produced is a 32 digit hexadecimal number, but we can parse it to an integer using the parseInt() function. We’ll get a large number, and by taking modulo 100, we can produce a value between 0 and 100.

1 Like

Yes, the fact that a modulo is applied to the hash (to effectively group them to n distinct buckets) is something that’s not explicitly stated and the article should be updated to clarify this. In fact, modulo is mentioned in the Naive example, which makes it seem like an unnecessary step in the solution.