找回密码
 快速注册
搜索
查看: 41|回复: 1

二次剩余伪随机数发生器

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2023-5-2 19:20 |阅读模式
readme中说
The math behind this is pure magic. Whoever figured this out, please explain lol.

我也想弄明白这个代码是什么原理
E、A、O必须是小于U的素数,其中E最大,A次之,O相对较小。
E 应该是小于 U 的最大素数。
U 可以是任何 bigint。
所有数字都必须是 bigint。
I 是一个递增的 bigint
  1. const permute = require('@lancejpollard/quadratic-residue-prng.js')
  2. const E = 3132343537383103113163n
  3. const A = 975319753197531975319n
  4. const O = 541613713n
  5. const U = 32 ** 15
  6. let i = 0
  7. while (i < U) {
  8.   const x = permute(i, E, A, O, U)
  9.   console.log(x)
  10.   i++
  11. }
复制代码

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-5-2 19:25

代码的解释 (不知背后的数学原理)

So for example, you want to generate 3 sets of pseudo random IDs, you're going to need 3 different sequences (output), but can use the same range of input sequences (0 to max U). Pick a unique A for each sequence, but fix E, O, and U. Then when you input i, you will get different values for each sequence at index i. Sometimes you might want to change U to be a different max, to limit the range (and size) of possible values, as well. But if U is constant, to generate different sequences, vary A on a per-sequence basis.

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 15:27

Powered by Discuz!

× 快速回复 返回顶部 返回列表