带种子的 Python 随机序列

发布于 2024-10-09 11:56:11 字数 166 浏览 2 评论 0原文

我正在为一个学校项目执行此操作(因此我无法使用任何高级功能)并且我正在使用 Python 2.6.6。

我有一个从 1 到 1000 的数字列表和一个种子(例如 448)。

如何使用该种子生成随机序列,以便列表中的数字位于不同的索引中?

知道种子后是否可以按原始顺序返回列表?

I'm doing this for a school project (so I can't use any advanced features) and I'm using Python 2.6.6.

I have a list of numbers from 1 to 1000 and a seed (448 for example).

How can I generate a random sequence with that seed so that the numbers in my list will be in a different index ?

And is it possible, knowing the seed, to return the list in its original order ?

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

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

发布评论

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

评论(2

夢归不見 2024-10-16 11:56:11
import random
SEED = 448

myList = [ 'list', 'elements', 'go', 'here' ]
random.seed(SEED)
random.shuffle(myList)

print myList

结果

['here', 'go', 'list', 'elements']

您的列表现在是伪随机的。

“伪”很重要,因为具有相同种子和项目数量的所有列表都将以相同的“随机”顺序返回。我们可以用它来取消您的列表的洗牌;如果它真的是随机的,这是不可能的。

Order = list(range(len(myList)))
# Order is a list having the same number of items as myList,
# where each position's value equals its index

random.seed(SEED)
random.shuffle(Order)
# Order is now shuffled in the same order as myList;
# so each position's value equals its original index

originalList = [0]*len(myList)   # empty list, but the right length
for index,originalIndex in enumerate(Order):
    originalList[originalIndex] = myList[index]
    # copy each item back to its original index

print originalList

结果在

['list', 'elements', 'go', 'here']

田田! OriginalList 现在是 myList 的原始排序。

import random
SEED = 448

myList = [ 'list', 'elements', 'go', 'here' ]
random.seed(SEED)
random.shuffle(myList)

print myList

results in

['here', 'go', 'list', 'elements']

Your list is now pseudorandomized.

'Pseudo' is important, because all lists having the same seed and number of items will return in the same 'random' order. We can use this to un-shuffle your list; if it were truly random, this would be impossible.

Order = list(range(len(myList)))
# Order is a list having the same number of items as myList,
# where each position's value equals its index

random.seed(SEED)
random.shuffle(Order)
# Order is now shuffled in the same order as myList;
# so each position's value equals its original index

originalList = [0]*len(myList)   # empty list, but the right length
for index,originalIndex in enumerate(Order):
    originalList[originalIndex] = myList[index]
    # copy each item back to its original index

print originalList

results in

['list', 'elements', 'go', 'here']

Tada! originalList is now the original ordering of myList.

遗心遗梦遗幸福 2024-10-16 11:56:11

对 python 文档的简单检查
http://docs.python.org/library/random.html
告诉您

random.seed([x])

可以使用哪些来初始化种子。

要再次按照您的首字母顺序获取项目,请再次设置种子并再次获取随机数。然后,您可以使用此索引来获取列表中的内容,或者仅使用该索引进行任何操作。

您只需对列表进行排序,它就会再次按排序顺序排列。

A simple check on the python docs
http://docs.python.org/library/random.html
tells you about

random.seed([x])

which you can use to initialize the seed.

To get the items in the order of your initial again, set the seed again and get the random numbers again. You can then use this index to get the content in the list or just use the index for whatever.

You’d just sort the list and it’d be in sorted order again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文