目录:
目的:理解random.seed(),通过代码探究并验证其功能。
探究:固定seed下对含任意数量(>1)元素的列表进行任意深度的shuffle排序测试:
目的:理解random.seed(),通过代码探究并验证其功能。
背景:random()是伪随机,实际上由确定公式根据初始值seed计算得出。
当seed固定时,对特定数据的处理会产生相同的结果。
试探究固定seed下random.shuffle()对字符串列表排序的影响:
趣味思考:例如 random.seed(43)时
对三元素列表使用random.shuffle,结果为:[a, b, c] → [c, b, a]
you are superman → superman are you (与list.reverse()效果相同)
对四元素列表使用random.shuffle,结果为:[a, b, c, d] → [c, d, b, a]
I am the king → the king I am
import random
sentence = input().split()
random.seed(43)
random.shuffle(sentence)
print(' '.join(sentence))