为 64 位 LCG 找到更多独立的种子值(MMIX(由 Knuth))

2024-01-03

I'm using a 64 bit LCG (MMIX (by Knuth)). It generate a certain block of random numbers inside my code, which use them to perform some operations. My code works in single core and I would like to parallelize the work to reduce the execution time.
Before start thinking to more advanced methods in this sense I'd like to simply execute more identical codes in parallel (in fact the code repeats the same task over a certain numbers of indipendent simulation, so I can simply split the number of simulation between more identical codes and run them in parallel).
My only problem now is to find a seed for each code; in particular, to avoid the possibility of unwanted non trivial correlation between data generated in different codes, I have to be sure that the random number generated in the various codes don't overlap. To do so, starting from a certain seed in the first code I have to find a way to find a value (the next seed) very distant not in absolute value but in the pseudo-random sequence (so, such that, to go from the first to the second seed, I need a huge number of steps of LCG).
My first attempt was this:
starting from the LCG relation between 2 consecutive numbers generated in the sequence
enter image description here
So, in principle, I could calculate the above relation with, say, n = 2^40 and I_0 equal to the value of the first seed, and obtain a new seed distant 2^40 steps in the random CLG sequence from the first one.
The problem is that, doing so, I necessary go in overflow calculating a^n. In fact for MMIX (by Knuth) a~2^62 and i use unsigned long long int (64 bit). Note that the only problem here is the fraction in the above relation. If there only were sum and multiplication I could ignore the overflow problem due to the following modular properties (in fact I'm using 2^64 as c (64 bit generator)):

那么,从某个值(第一个种子)开始,我怎样才能在 LC 伪随机序列中找到距离大量步长的第二个种子呢?

[EDIT]
r3mainer solution is perfectly suited for python codes. I'm trying now to implement it in c using unsigned __int128 variables. I have only one problem: in principle I should compute:
enter image description here

假设,为了简单起见,我想计算:

其中 n = 2^40 和 c(a-1)~2^126。我继续一个循环。从temp = a,在每次迭代中我计算temp = temp*temp,然后我计算temp%c(a-1)。问题出在第二步(temp = temp*temp). temp事实上,原则上可以是任何 temp是一个很大的数字,比如说 > 2^64,我会溢出,在下一个模块操作之前达到 2^128 - 1。那么有没有办法避免呢?目前,我看到的唯一解决方案是通过位循环执行每个乘法,如下所示:c代码:防止在具有巨大模块的模块化操作中溢出(模块接近溢出阈值) https://stackoverflow.com/questions/66180370/c-code-prevent-overflow-in-modular-operation-with-huge-modules-modules-near-th还有另一种方法可以在乘法期间执行模运算吗?
(请注意,c = 2^64,使用 mod(c) 操作我没有相同的问题,因为溢出点(对于 ull int 变量)与模块一致)


任何以下形式的 LCGx[n+1] = (x[n] * a + c) % m可以非常快速地跳到任意位置。

从种子值为零开始,LCG 的前几次迭代将给出以下序列:

x₀ = 0
x₁ = c % m
x₂ = (c(a + 1)) % m
x₃ = (c(a² + a + 1)) % m
x₄ = (c(a³ + a² + a + 1)) % m

很容易看出,每一项实际上都是几何级数的总和,可以用以下公式计算:简单的公式 https://en.wikipedia.org/wiki/Geometric_series#Sum:

x_n = (c(a^{n-1} + a^{n-2} + ... + a + 1)) % m
    = (c * (a^n - 1) / (a - 1)) % m

The (a^n - 1)项可以通过以下方式快速计算模幂 https://en.wikipedia.org/wiki/Modular_exponentiation,但除以(a-1)有点棘手,因为(a-1) and m都是偶数(即不互质),所以我们无法计算模乘逆 https://en.wikipedia.org/wiki/Modular_multiplicative_inverse of (a-1) mod m直接地。

相反,计算(a^n-1) mod m*(a-1),然后对结果进行直接(非模块化)除法a-1。在 Python 中,计算过程如下:

def lcg_skip(m, a, c, n):
    # Calculate nth term of LCG sequence with parameters m (modulus),
    # a (multiplier) and c (increment), assuming an initial seed of zero
    a1 = a - 1
    t = pow(a, n, m * a1) - 1
    t = (t * c // a1) % m
    return t

def test(nsteps):
    m = 2**64
    a = 6364136223846793005
    c = 1442695040888963407
    #
    print("Calculating by brute force:")
    seed = 0
    for i in range(nsteps):
        seed = (seed * a + c) % m
    print(seed)
    #
    print("Calculating by fast method:")
    # Calculate nth term by modular exponentiation
    print(lcg_skip(m, a, c, nsteps))

test(1000000)

因此,要创建具有不重叠输出序列的 LCG,您需要做的就是使用由lcg_skip()值为n相距足够远。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为 64 位 LCG 找到更多独立的种子值(MMIX(由 Knuth)) 的相关文章

随机推荐

  • Spotify 的自定义网络播放器

    据我所知 不可能开发一个Web应用程序 在spotify com之外 提供播放Spotify歌曲的自定义Web播放器 对吗 唯一的选择似乎仍然是 Spotify 播放按钮 但功能非常有限 然而我刚刚看到这个例子 1 http static
  • 如何应对 R 中的“非数字矩阵范围”错误?

    我正在尝试使用标准随机方程从学生的 t 分布生成模拟值的数据框 我使用的函数如下 matgen lt function means chi covariancematrix cols lt ncol means normals lt mvr
  • 如何使用 Python 在 Spark 中添加两个稀疏向量

    我到处搜索 但找不到如何使用 Python 添加两个稀疏向量 我想添加两个稀疏向量 如下所示 1048576 110522 0 6931 521365 1 0986 697409 1 0986 725041 0 6931 749730 0
  • Grails 3.2 JSON 视图中的 Java 8 LocalDate

    当我尝试使用 a 来解析包含 Java 8 LocalDateTime 的简单域模型对象时json视图 http docs grails org latest guide single html whatsNewJSONViews 我收到一
  • 如何使用反射调用自定义运算符

    在我的小项目中我使用System Reflection类来生成可执行代码 我需要打电话给 自定义类型的运算符 有谁知道如何使用 C 反射调用自定义类的自定义运算符 C 编译器将重载运算符转换为具有名称的函数op XXXX where XXX
  • 如何处理 .ajax post 中的 FileStream 返回类型?

    我通过以下代码发送 JSON 对象 控制器返回 CSV 格式的值 应提示打开或保存为 CSV 文件 我无法理解成功应该编写什么代码 函数 结果 导出链接 Html ActionLink Export null new onclick ret
  • 如何让 Java 应用程序与网站交互

    我有一个程序 可以从 Excel 文件中获取数据并为用户进行操作 但为了获取 Excel 文件的更新 需要从网站下载它们 我最初尝试使用机器人类导航到网站 使用用户名和密码登录 然后导航到网站的正确部分 找到 下载 Excel 电子表格 按
  • “所选目录不是 JDK 的有效主目录”Android Studio

    我一直在使用安卓工作室直到我更新到0 2 6 现在 我无法编译或创建新项目 会发生什么 我不确定 但我认为安卓工作室不知道我的 sdk 文件夹在哪里 我的意思是我的 android studio 目录中的 sdk 文件夹 我做了什么 我已经
  • Matplotlib vline 标签参数未显示

    我想用 matplotlib 的 vline 命令标记垂直线 但由于某种原因 label 参数在最终绘图上不执行任何操作 显示任何内容 有谁知道如何让标签显示出来 plt vlines x pah ymin 0 ymax 0 6 color
  • graphviz - 比较图形树

    我必须用 graphviz 来做一份工作 我需要可视化几棵树的图形表示 但无论如何我都必须比较两棵树以查看它们的差异 像这样 我有树 A 和树 B 创建它们的表示并比较它们后 我只需要查看没有共同点的节点 有人告诉我使用 EMF Compa
  • 如何在 TFS 构建定义中获取签入评论/消息?

    是否可以在 TFS 2013 构建定义 工作流程文件 中获取签入注释 消息 我看过BuildDetail但还没找到 注释是 a 的属性 a变更集 http msdn microsoft com en us library microsoft
  • 当锚标记仅触发 jQuery 操作而不重定向用户时,可以替代

    我的页面上有许多锚标记 它们仅触发同一页面上的 jQuery 操作 不会将用户重定向到另一个位置 这是锚标记的正常预期行为 我不想在我的应用程序中为每个操作都提供静态 URL 但是 我也不喜欢每次用户单击其中一个时都将其发送到页面顶部 a
  • 如何使椭圆跟随画布上的曲线

    我在尝试让椭圆正确遵循画布上的路径时遇到问题 我认为问题源于这样一个事实 我的迷你语法定义了 x 和 y 值之间的移动 但仅针对目标属性中的这些值之一 例如 Canvas Top or Canvas Left 我似乎在画布上找不到任何附加的
  • 我可以自动保存正在运行的 jupyter python 笔记本而不在浏览器选项卡中打开它吗?

    所以我有一个长期运行的Python笔记本 只要在我的浏览器选项卡中打开它 它就会每 2 分钟自动保存一次 生活很美好 即使我关闭浏览器选项卡 是否可以保持自动保存 当我关闭选项卡时 内核已经继续运行 这很棒 这有点像 屏幕 但是在 jupy
  • 模板参数上的 C++ 函数模板重载

    是否可以像这样重载函数模板 仅在使用enable if的模板参数上 template
  • Gradle:强制自定义任务始终运行(无缓存)

    我编写了一个自定义 Gradle 任务来处理路径可配置的文件系统上的一些依赖项解析 我希望这种类型的任务始终运行 我猜它们似乎只运行一次 因为输入似乎永远不会改变 我知道使用configurations resolutionStrategy
  • 没有声明为 public、private 或 protected 的变量是什么?

    如果代替 private JButton theButton 我这样定义一个字段 JButton theButton 有什么不同 Package
  • 如何通过推送通知打开 ios 应用程序?

    我可以知道如何打开 ios 应用程序 点击通知 或者当我们滑动通知图标时 如果 iPhone 被锁定 有人可以帮我吗 当点击通知时 操作系统会处理该行为 无论通知是否由第 3 方发送 如果它通过 APNS 它将打开应用程序并在内部appli
  • 如何在Linux中截图(高fps)(编程)

    首先我想说我已经阅读了很多关于这方面的内容并且学到了很多方法来做到这一点 但是我还没有能够在linux中做到这一点 我的项目是一个带有arduino的流光溢彩的项目 所以我需要截取桌面的屏幕截图并分析它的颜色 一开始 我使用Processi
  • 为 64 位 LCG 找到更多独立的种子值(MMIX(由 Knuth))

    I m using a 64 bit LCG MMIX by Knuth It generate a certain block of random numbers inside my code which use them to perf