Pseudo-random-looking one-to-one int32->int32 function

人走茶凉 提交于 2019-11-30 09:37:16

Multiply by a large odd number and xor with a different one.

Bijection: odd numbers have a multiplicative inverse modulo powers of two, so the multiplication is undone by a multiplication by the inverse. And xor is, of course, undone by another xor.

This is basically how the linear congruence pseudo random number generator works.

Probably an overkill for this task, but have you consider applying any crypto pseudo random permutation or other primitives comes from block ciphers. For example, it may be done using des with known key in counter mode:

younumber xor (des (key, number counter))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!