Search code examples
javarangechecksumcrc32

Generate a 1-5 number from a byte array


I have the need to consistently generate a number between 1-5 from a byteArray input.

I was thinking to use a CRC32 checksum so I have a long number.

I would need to translate the long to something between 1-5.

Is it a good solution? How could I achieve this transformation in a consistent way in Java?

Thanks

Update: to better understand my requirements: I have a set of images on my website and I want these to be served by multiple subdomains to parallelize browser downloads. I'm going to have 5 subdomains serving the same images. I want one image to be requested only to one subdomain to use browser caching consistently. more here Reduce site load time with multiple CDN sub-domains


Solution

  • If you want a number that depends on the contents of the array, you can use:

    int yourNumber = Arrays.hashCode(yourArray) % 5 + 1;