Forgot password?
 Create new account
View 109|Reply 1

二次剩余伪随机数发生器

[Copy link]

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

hbghlyj Posted at 2023-5-2 19:20:55 |Read mode
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. }
Copy the Code

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

 Author| hbghlyj Posted at 2023-5-2 19:25:13

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

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.

手机版Mobile version|Leisure Math Forum

2025-4-20 21:57 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list