dongwu8064 2011-09-05 07:58
浏览 115
已采纳

算法将md5(或者可能是另一种散列方法?)转换为整数,可以设置可能的结果整数范围(例如:1-10000)?

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

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

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

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?

final note: the range will always start with 1.

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

thanks!

  • 写回答

2条回答 默认 最新

  • duanju9104 2011-09-05 08:02
    关注

    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.

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来