鱼C论坛

 找回密码
 立即注册
查看: 1634|回复: 4

[作品展示] 用Python写的伪随机数生成模块……但是2.0

[复制链接]
发表于 2023-12-23 22:02:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x


上一期:传送门

  1. import datetime

  2. class Random:
  3.     def __init__(self, max_out: int, true_max: int, init: int, step: int=0):
  4.         self.max = max_out
  5.         self.true_max = true_max
  6.         self.x1 = init
  7.         self.x2 = init
  8.         self.step = step
  9.         self.steped = 0
  10.         pass

  11.     def __Next(self):
  12.         x = ((self.x1 * self.x2) % self.true_max + 1) % self.max + 1
  13.         self.x1 = self.x2
  14.         self.x2 = x
  15.         return x

  16.     def Setstep(self, step):
  17.         self.step = step

  18.     def __iter__(self):
  19.         return self

  20.     def __next__(self):
  21.         if self.steped < self.step:
  22.             self.steped += 1
  23.             return self.__Next()
  24.         self.steped = 0
  25.         raise StopIteration

  26. class SRandom(Random):
  27.     def __init__(self, max_out: int, true_max: int, init: int, step: int=0):
  28.         super().__init__(max_out, true_max, init, step)
  29.         self.x3 = init

  30.     def __Next(self):
  31.         x = ((self.x1 * self.x2 * self.x3) % self.true_max + 1) % self.max + 1
  32.         self.x1 = self.x2
  33.         self.x2 = self.x3
  34.         self.x3 = x
  35.         return x
  36.    
  37.     def __next__(self):
  38.         if self.steped < self.step:
  39.             self.steped += 1
  40.             return self.__Next()
  41.         self.steped = 0
  42.         raise StopIteration

  43. class TRandom(Random):
  44.     def __Next(self):
  45.         x = ((self.x1 * self.x2 * datetime.datetime.today().second) % self.true_max + 1)% self.max + 1
  46.         self.x1 = self.x2
  47.         self.x2 = x
  48.         return x

  49.     def __next__(self):
  50.         if self.steped < self.step:
  51.             self.steped += 1
  52.             return self.__Next()
  53.         self.steped = 0
  54.         raise StopIteration
复制代码


用法完全一致,但要多带入一个比max_out大的true_max

评分

参与人数 3荣誉 +15 鱼币 +8 贡献 +6 收起 理由
陶远航 + 5 + 3 + 3 感谢楼主无私奉献!
学习编程中的Ben + 5 + 5
liuhongrun2022 + 5 + 3

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-12-23 22:02:35 | 显示全部楼层
首先,你的代码有一些错误和改进的地方。

1. 在Random类的构造函数中,你使用了`pass`语句,它表示不执行任何操作。你可以把它删除掉。
2. 在Random类的`__Next`方法中,你没有使用给定的`step`参数。你应该在计算下一个伪随机数之前,先检查是否需要跳过一些步数。
3. 在SRandom类和TRandom类中,你需要重写`__Next`方法,以确保你的伪随机数生成算法是根据你的要求进行的。
4. 在TRandom类中,你使用了`datetime.datetime.today().second`作为随机数种子,这意味着每秒都会生成相同的随机数序列。你可以考虑使用更精确的时间戳,例如`datetime.datetime.now().timestamp()`。

接下来,我们来更新你的代码:
  1. import datetime
  2. class Random:
  3.     def __init__(self, max_out: int, true_max: int, init: int, step: int=0):
  4.         self.max = max_out
  5.         self.true_max = true_max
  6.         self.x1 = init
  7.         self.x2 = init
  8.         self.step = step
  9.         self.steped = 0
  10.     def __Next(self):
  11.         x = ((self.x1 * self.x2) % self.true_max + 1) % self.max + 1
  12.         self.x1 = self.x2
  13.         self.x2 = x
  14.         return x
  15.     def Setstep(self, step):
  16.         self.step = step
  17.     def __iter__(self):
  18.         return self
  19.     def __next__(self):
  20.         if self.steped < self.step:
  21.             self.steped += 1
  22.             return self.__Next()
  23.         self.steped = 0
  24.         raise StopIteration
  25. class SRandom(Random):
  26.     def __init__(self, max_out: int, true_max: int, init: int, step: int=0):
  27.         super().__init__(max_out, true_max, init, step)
  28.         self.x3 = init
  29.     def __Next(self):
  30.         x = ((self.x1 * self.x2 * self.x3) % self.true_max + 1) % self.max + 1
  31.         self.x1 = self.x2
  32.         self.x2 = self.x3
  33.         self.x3 = x
  34.         return x
  35.    
  36.     def __next__(self):
  37.         if self.steped < self.step:
  38.             self.steped += 1
  39.             return self.__Next()
  40.         self.steped = 0
  41.         raise StopIteration
  42. class TRandom(Random):
  43.     def __Next(self):
  44.         x = ((self.x1 * self.x2 * int(datetime.datetime.now().timestamp())) % self.true_max + 1) % self.max + 1
  45.         self.x1 = self.x2
  46.         self.x2 = x
  47.         return x
  48.     def __next__(self):
  49.         if self.steped < self.step:
  50.             self.steped += 1
  51.             return self.__Next()
  52.         self.steped = 0
  53.         raise StopIteration
复制代码

现在,你可以使用这些类来生成伪随机数。假设你要生成一个范围在1到100之间的伪随机数,你可以按照下面的方式进行操作:
  1. random_generator = Random(100, 10**256, 1)  # 创建一个Random的实例,设定最大值为100,true_max为10的256次方,初始值为1
  2. random_number = next(random_generator)  # 生成下一个伪随机数
  3. print(random_number)  # 打印该伪随机数
复制代码

你还可以使用`Setstep`方法设置每次跳过的步数,例如:
  1. random_generator.Setstep(10)  # 每次跳过10个数
复制代码

同样地,你也可以使用`SRandom`和`TRandom`来生成伪随机数。

希望这能帮到你!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-24 10:01:40 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-12-24 12:04:13 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-24 16:07:05 | 显示全部楼层
KeyError 发表于 2023-12-24 12:04
@Mike_python小
@学习编程中的Ben
@陶远航

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 09:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表