Python随机数

Python提供random模块用于生成伪随机数

1.随机数种子

在Python中,随机数种子(random seed)是一个用于初始化随机数生成器的整数值。设置随机数种子可以确保每次运行程序时生成的随机数序列都是相同的,从而使得结果具有可重复性。
通过设置随机数种子,可以控制Python随机函数的行为。这对于调试和测试程序非常有用,因为可以确保每次运行程序时获得相同的结果,从而更容易发现问题并进行调试。
Python的随机数生成器默认不设置种子值,以确保生成的随机数在实际应用中具有随机的特性。

random.seed(a=None, version=2)

Initialize the random number generator.

If a is omitted or None, the current system time is used. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

If a is an int, it is used directly.

2.随机数生成函数

Functions for integers
  • random.randrange(stop)
  • random.randrange(start, stop[, step]):Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.
  • random.randint(a, b):Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).
Functions for sequences
  • random.choice(sequence): 用于从序列中随机选择一个元素
  • random.shuffle(sequence): 用于将序列中的元素进行随机排序\footnote{直接改变sequence的值}
Real-valued distributions
  • random.random():Return the next random floating point number in the range [0.0, 1.0).
  • random.uniform(a, b):Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.The end-point value b may or may not be included in the range depending on floating-point rounding in the equation a + (b-a) * random().
  • random.gauss(mu, sigma):Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function defined below.

3.Numpy中的随机数

参考文献:python 3.9.13 documentation

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
下一篇