dqajyxqem115006813 2011-06-10 05:13
浏览 33
已采纳

如何根据密码生成数组的混洗子集,以便相同的密码输出相同的子集?

I have an array in my PHP application, and I need to develop a function that, given the array and an arbitrary string password, will output a shuffled subset of the large array. The subset and order of the array outputted should be the same every time the same password is used to call the function.

I can generate a pseudo-random number based on the password by taking the integer value of a hash like MD5, but I'm not sure how I can use this to then:

1) Select X (either a fixed number or, preferably, a number chosen by the hash within a given range) random items from the array based on the hash

2) Shuffle/rearrange these items into a subset based on the hash

For my implementation collisions are acceptable, as the array is of a relatively small size. This is a pretty interesting problem - anyone have any idea how to tackle it?

  • 写回答

1条回答 默认 最新

  • douluanji8752 2011-06-10 05:28
    关注

    Don't know much about the PHP implementation, but I think the general algorithm would involve seeding a pseudo-random number generator using your hash.

    Seeding a pseudo-random number generator ensures that the pseudo-random numbers you get are in the same order every time. Shuffle the array using the first pseudo-random number from the seeded generator. Then, use the next value of the pseudo-random number generator to pick a number between 1 and the size of your array; slice off these first n elements from the array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里