博客
关于我
随机变量生成算法入门——Wichmann-Hill算法
阅读量:375 次
发布时间:2019-03-05

本文共 868 字,大约阅读时间需要 2 分钟。

Wichmann-Hill算法

算法介绍

Wichmann-Hill算法是一种生成长周期随机数的方法。其基本思想是将两个周期相差很小的波形序列叠加,通过这种方式可以生成一个更长周期的随机数发生器。具体来说,定义三个随机数发生器,它们都是全周期的。然后将它们合成输出,形成一个等价的乘性发生器。

C++代码实现

#include 
int main() { float x[10001], y[10001], z[10001], u[10001]; x[0] = 1; y[0] = 2; z[0] = 3; // 种子可取任意整数 for (int i = 0; i < 10000; ++i) { x[i+1] = fmod(171 * x[i], 30269); y[i+1] = fmod(170 * y[i], 30307); z[i+1] = fmod(172 * z[i], 30323); u[i+1] = fmod(x[i+1] / 30269 + y[i+1] / 30307 + z[i+1] / 30323, 1); } FILE *fp; fp = fopen("/*输入自定义路径*/", "w"); for (int i = 1; i <= 10000; ++i) { fprintf(fp, "%f ", u[i]); } fclose(fp); return 0;}

使用MATLAB画直方图

将C++生成的文件导入MATLAB后,使用histogram函数绘制直方图即可。以下是具体步骤:

  • 确保MATLAB能够识别文件路径。
  • 使用import命令导入数据。
  • 调用histogram函数并设置适当的参数。
  • 通过这些步骤,可以直观地查看随机数分布的情况。

    生成直方图预览

    此外,以下直方图展示了生成的随机数在[0,1)区间内的均匀分布特性。

    转载地址:http://wpdg.baihongyu.com/

    你可能感兴趣的文章
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>