Forgot password
 Register account
View 152|Reply 1

二次剩余伪随机数发生器

[Copy link]

3175

Threads

7937

Posts

48

Reputation

Show all posts

hbghlyj posted 2023-5-2 19:20 |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

3175

Threads

7937

Posts

48

Reputation

Show all posts

original poster hbghlyj posted 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.

Quick Reply

Advanced Mode
B Color Image Link Quote Code Smilies
You have to log in before you can reply Login | Register account

$\LaTeX$ formula tutorial

Mobile version

2025-7-1 03:28 GMT+8

Powered by Discuz!

Processed in 0.019292 seconds, 43 queries