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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号