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.

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

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路