算法转换成MD5(或者另一种散列法?)为整数的地方可以设置可能产生的整数范围(例如:1-10000)?整数、转换成、算法、范围

2023-09-11 06:18:59 作者:熬过去就好了

主题pretty的太多描述了,我们想实现的目标。

the topic pretty much describes what we would like to accomplish.

一)开始与整数的可能范围,例如,1至10000。

a) start with a possible range of integers, for example, 1 to 10000.

二)采取任何MD5哈希值,运行它通过这个算法中。

b) take any md5 hash, run it thru this algo.

C)结果弹出将是10000 1之间的一个整数。

c) result that pops out will be an integer between 1 to 10000.

我们是开放的使用另一种散列方法了。

we are open to using another hashing method too.

流量在理想情况下是这样的:

the flow would ideally look like this:

string -> md5(string) -> algo(md5(string),range) -> resulting integer within range

是这样的可能吗?

is something like this possible?

最后要注意的范围将总是以1

final note: the range will always start with 1.

如果你有一个答案,随意张贴只是一般的想法,如果你愿意的话,PHP代码段工程太:)

if you have an answer, feel free to post just the general idea, or if you so desire, php snippet works too :)

谢谢!

推荐答案

由于MD5(和SHA-1等),会给你128位数据(在PHP中,你会得到它的十六进制字符串表示法,所以你需要将其先转换为整数)。这一数字模10000会给你的整数。

Since MD5 (and SHA-1, etc.) will give you 128 bits of data (in PHP, you'll get it in hexadecimal string notation, so you need to convert it to an integer first). That number modulo 10000 will give you your integer.

请注意然而,很多不同的哈希值会转换为相同的整数;这是不可避免的带的任何的排序转换到您的整数范围,作为操作基本上映射的一组数字的更大(在这种情况下,128位,即从0到340,282,366,920,938,463,463,374,607,431,768,211,456)为一组数字(小于17位,数字小为1〜10万)。

Note however that many different hashes will convert to the same integer; this is unavoidable with any sort of conversion to your integer range, as the modulo operation essentially maps a larger set of numbers (in this case, 128 bits, that is numbers from 0 to 340,282,366,920,938,463,463,374,607,431,768,211,456) to a smaller set of numbers (less than 17 bits, numbers from 1 to 100,000).