如何使用可逆种子使所有可能的列表组合?

发布于 2025-02-09 03:29:21 字数 77 浏览 1 评论 0原文

我想拥有一个由100个项目制成的列表,每个项目都是0-31的值。然后,我希望能够获取以下列表之一,并知道随机生成该确切列表的必要种子/输入。

I want to have a list made out of 100 items, each item being a value from 0-31 inclusive. I then want to be able to take one of these lists, and know what seed/input is necessary to randomly generate that exact list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2025-02-16 03:29:21

使用一些适当的线性一致生成器:

您可以使用Pierre L'Ecuyer的这篇研究论文:

表rer” > LCGS IS 2 30 ,接近10亿。参见论文的表4。只需选择其中一个LCG,例如:

u n + 1 =((((116646453) * un ) + 5437)mod 2 30

您的物品正好宽5位。如果您决定将项目6乘6分组,则每个 group 恰好宽30位,因此可以被视为此Modulo 2 30 lcg的一个状态。

从最初的6个项目组中,LCG的一个步骤将生成下一个组,即接下来的6个项目。论文告诉您,意甲总体上看起来是合理的随机。

因此,您可以将前6个项目视为“种子”,因为您可以从最左边的6个项目中重建整个列表。

即使假设为了混淆,您在种子之后启动了列表的可见部分,您仍然只有大约十亿个可能的种子可以担心。任何体面的计算机都可以通过少数几秒钟的时间在几秒钟内通过蛮力找到剩下的种子,并通过为每种可能的种子模拟LCG并与实际列表进行比较。

示例Python代码:

一个人可以从创建一个类别的类中开始,该类给定种子,提供了一个无限的意甲,在0到31之间的项目:

class LEcuyer30:
   def  __init__ (self, seed):
       self.seed      = seed & ((1 << 30) - 1)
       self.currGroup = self.seed
       self.itemCount = 6

   def __toNextGroup(self):
       nextGroup = ((116646453 * self.currGroup) + 5437) % (1 << 30)
       self.currGroup = nextGroup
       self.itemCount = 6

   def  getItem(self):
       if (self.itemCount <= 0):
           self.__toNextGroup()

       # extract 5 bits:
       word = self.currGroup >> (5 * (self.itemCount - 1))
       self.itemCount -= 1
       return (word & 31)

测试代码:

我们可以创建20个项目的序列并打印它:

# Main program:

randomSeed = 514703103
rng = LEcuyer30(randomSeed)

itemList = []
for k in range(20):
    item = rng.getItem()
    itemList.append(item)

print("itemList = %s" % itemList)

程序输出:输出:

itemList = [15, 10, 27, 15, 23, 31, 1, 10, 5, 15, 16, 8, 4, 16, 24, 31, 7, 5, 8, 19]

Using some suitable Linear Congruential Generator:

You could use this research paper by Pierre L'Ecuyer:

Tables_of_linear_congruential_generators_of_different_sizes_and_good_lattice_structure

The lowest power of 2 modulus for which the paper gives examples of (decently pseudo-random) LCGs is 230, which is close to 1 billion. See table 4 of the paper. Just pick one of those LCGs, say:

un+1 = ((116646453 * un) + 5437) mod 230

Each of your items is exactly 5 bits wide. If you decide to group your items 6 by 6, each group is exactly 30 bits wide, so can be considered as one state of this modulo 230 LCG.

From a initial group of 6 items, one step of the LCG will generate the next group, that is the next 6 items. And the paper tells you that the serie will look reasonably random overall.

Hence, you can regard the first 6 items as your “seed”, as you can reconstruct the whole list from its leftmost 6 items.

Even assuming that for the sake of obfuscation you started the visible part of the list after the seed, you would still have only about one billion possible seeds to worry about. Any decent computer would be able to find the left-hidden seed by brute force within a handful of seconds, by simulating the LCG for every possible seed and comparing with the actual list.

Sample Python code:

One can start by creating a class that, given a seed, supplies an unlimited serie of items between 0 and 31:

class LEcuyer30:
   def  __init__ (self, seed):
       self.seed      = seed & ((1 << 30) - 1)
       self.currGroup = self.seed
       self.itemCount = 6

   def __toNextGroup(self):
       nextGroup = ((116646453 * self.currGroup) + 5437) % (1 << 30)
       self.currGroup = nextGroup
       self.itemCount = 6

   def  getItem(self):
       if (self.itemCount <= 0):
           self.__toNextGroup()

       # extract 5 bits:
       word = self.currGroup >> (5 * (self.itemCount - 1))
       self.itemCount -= 1
       return (word & 31)

Test code:

We can create a sequence of 20 items and print it:

# Main program:

randomSeed = 514703103
rng = LEcuyer30(randomSeed)

itemList = []
for k in range(20):
    item = rng.getItem()
    itemList.append(item)

print("itemList = %s" % itemList)

Program output:

itemList = [15, 10, 27, 15, 23, 31, 1, 10, 5, 15, 16, 8, 4, 16, 24, 31, 7, 5, 8, 19]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文