主要内容

RNG

控制随机数生成器

描述

example

rng(seed)指定MATLAB的种子®随机数生成器。例如,rng(1)使用一种种子初始化Mersenne Twister Generator1

TheRNGfunction controls the全球流, which determines how therand,randi,randn, 和randpermfunctions produce a sequence of random numbers. To create one or more independent streams separate from the global stream, seeRandStreamRandStream.create

example

rng(seed,发电机)also specifies the type of random number generator to use. For example,rng(0,'philox')初始化Philox 4x32随机发生器,种子的种子0

example

s =RNG返回结构中的当前随机数生成器设置s

例子

全部收缩

将随机数生成器设置为默认种子(0) and algorithm (Mersenne Twister), then save the generator settings.

rng('默认') s = rng
s =带有字段的结构:类型:'Twister'种子:0状态:[625x1 UINT32]

Create a 1-by-5 row vector of random values between 0 and 1.

x = rand(1,5)
x =1×50.8147 0.9058 0.1270 0.9134 0.6324

Change the generator seed and algorithm, and create a new random row vector.

rng(1,'Philox')Xnew = rand(1,5)
Xnew =1×50.5361 0.2319 0.7753 0.2390 0.0036

Now restore the original generator settings and create a random vector. The result matches the original row vectorxcreated with the default generator.

rng(s) xold = rand(1,5)
xold =1×50.8147 0.9058 0.1270 0.9134 0.6324

Input Arguments

全部收缩

发电机初始化, specified as one of the following options.

价值 描述
0 Initializes generator with seed0
positive integer 用指定的正整数种子初始化发电机,例如1
'默认' Initializes Mersenne Twister generator with seed0。这是每个MATLAB会话开始时的默认设置。
'shuffle' 根据当前时间初始化生成器,导致每次调用到RNG
结构体 Initializes generator based on the settings contained in a structure with fields类型,Seed, 和State

随机数算法, specified as one of the options in the table. For more information on generator algorithms, seeCreating and Controlling a Random Number Stream

价值 Generator Name Generator Keyword
'twister' Mersenne Twister MT19937AR
'simdTwister' 面向SIMD的快速Mersenne Twister DSFMT19937
'combRecursive' 组合多个递归 MRG32K3A
“ multfibonacci' 乘法滞后的纤维科 mlfg6331_64
'Philox' Philox 4x32 generator with 10 rounds philox4x32_10
'threefry' 三fry4x64发电机,有20发子弹 trixfry4x64_20

For legacy generators used in MATLAB versions 4.0 and 5.0, use one of these options.

价值 Generator Name Generator Keyword
'v4' 传统MATLAB版本4.0发电机 MCG16807
'v5uniform' Legacy MATLAB version 5.0 uniform generator SWB2712
'V5normal' 传统MATLAB版本5.0普通发电机 shr3cong

提示

  • When you perform parallel processing, do not useRNG(“洗牌”)to set the random number stream on different workers to ensure independent streams since it seeds the random number generator based on the current time. This is especially true when the command is sent to multiple workers simultaneously, such as inside aparforjob. For independent streams on the workers, use the default behavior or consider using a unique substream on each worker usingRandStream

  • When you perform parallel processing, the default random number generators on the MATLAB client and MATLAB workers are different. If you need to generate the same random stream of numbers on the client and workers, you can useRNGwith the same generator type and seed (or consider usingRandStream具有相同的发电机类型,种子和正常转化算法)。有关更多信息,请参阅Control Random Number Streams on Workers(Parallel Computing Toolbox)

  • To useRNGinstead of therand或者randnfunctions with the'种子','状态', or'twister'inputs, see替换Rand和Randn的灰心语法

Extended Capabilities

Version History

在R2011a中引入