在Matlab函数中更改随机数生成器 [英] Change the random number generator in Matlab function

查看:51
本文介绍了在Matlab函数中更改随机数生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要完成一项需要输入准随机数的任务,但是我注意到我要使用的Matlab函数没有选择要使用的任何准发生器的选项(例如Halton,Sobol, 等等.).Matlab将它们作为独立功能,而不是普遍存在的"randn"和"rng"功能中的选项.MatLab使用的是Mersenne Twister,它是一个伪生成器.因此,例如,copularnd使用基于伪随机数的'randn'/'rng'....

I have a task to complete that requires quasi-random numbers as input, but I notice that the Matlab function I want to use does not have an option to select any of the quasi generators I want to use (e.g. Halton, Sobol, etc.). Matlab has them as stand alone functions and not as options in the ubiquitous 'randn' and 'rng' functions. What MatLab uses is the Mersenne Twister, a pseudo generator. So for instance the copularnd uses 'randn'/'rng' which is based on pseudo random numbers....

是否可以将它们合并到其他代码(例如 copularnd )中嵌入的 rand rng 函数中?任何指针将不胜感激.笔记;"copularnd"调用"mvnrnd",后者依次使用"randn",然后拉出"rng" ...

Is there a way to incorporate them into the rand or rng functions embedded in other code (e.g.copularnd)? Any pointers would be much appreciated. Note; 'copularnd' calls 'mvnrnd' which in turn uses 'randn' and then pulls 'rng'...

推荐答案

首先,您需要使用

First you need to initialize the haltonset using the leap, skip, and scramble properties. You can check the documents but the easy description is as follows:

  • 加扰-用于对点进行混排
  • 跳过-帮助从集合中排除一定范围的点
  • Leap (跳跃)-是从当前选定点到下一个跳跃点的大小.中间的点将被忽略.
  • Scramble - is used for shuffling the points
  • Skip - helps to exclude a range of points from the set
  • Leap - is the size of jump from the current selected point to the next one. The points in between are ignored.

现在,您可以构建一个 haltonset 对象:

Now you can built a haltonset object:

p = haltonset(2,'Skip',1e2,'Leap',1e1);
p = scramble(p,'RR2');

通过跳过前100个数字并跳过10个数字来设置二维哈尔顿数字.加扰方法是在第二行中应用的"PR2".您会看到生成了许多点:

This makes a 2D halton number set by skipping the first 100 numbers and leaping over 10 numbers. The scramble method is 'PR2' which is applied in the second line. You can see that many points are generated:

p = 

Halton point set in 2 dimensions (818836295885536 points)

Properties:
              Skip : 100
              Leap : 10
    ScrambleMethod : RR2

当您拥有 haltonset 对象p时,只需选择它们即可访问这些值:

When you have your haltonset object, p, you can access the values by just selecting them:

x = p(1:10,:)

注意:因此,您需要先创建对象,然后使用生成的点.要获得不同的结果,可以使用该函数的Leap和Scramble属性.您可以做的另一件事是使用均匀分布(例如 randi )每次从生成的点中选择数字.这样可以确保您每次都访问数据集的统一随机部分.

Notice: So, you need to create the object first and then use the generated points. To get different results, you can play with Leap and Scramble properties of the function. Another thing you can do is to use a uniform distribution such as randi to select numbers each time from the generated points. That makes sure that you are accessing uniformly random parts of the dataset each time.

例如,您可以生成一个随机索引向量(在此示例中为4点).然后使用它们从 halton 点中选择点.

For instance, you can generate a random index vector (4 points in this example). And then use those to select points from the halton points.

>> idx = randi(size(p,1),1,4)

idx =

   1.0e+14 *

    3.1243    6.2683    6.5114    1.5302

>> p(idx,:)

ans =

    0.5723    0.2129
    0.8918    0.6338
    0.9650    0.1549
    0.8020    0.3532

这篇关于在Matlab函数中更改随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆