伪随机数发生器 [英] Pseudo random number generator

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

问题描述

我正在尝试在MATLAB中实现伪随机数生成器.我正在根据以下配方进行构建:

I'm trying to implement a pseudo random number generator in MATLAB. I'm building it according to the following recipe:

  1. 创建随机数生成器种子x_0.将x_0的8 ... 15位保存到名为random_numbers的单元格中.

  1. Create the random number generator seed, x_0. Get and save bits 8...15 of x_0 to the cell named random_numbers.

For循环:使用rem((25173 * previous_number + 13849)+ 2 ^ 16))创建下一个n个数字.将编号n的8 ... 15位保存到名为random_numbers的单元中.打印编号n.

For Loop: Create the next n numbers using rem( (25173 * previous_number + 13849) + 2^16) ). Save bits 8...15 of number n to the cell named random_numbers. Print number n.

第1步:

n = 2000;

% Pre-allocate cell with size n numbers
random_numbers = cell(1, n);

% Generate seed in range 1 - 2^16 - 1
x_0 = randi([1, 2^16-1]);

% Save seed as binary array of size 16 bits
x_0 = de2bi(x_0, 16);

% Extract bits 8...15 from seed
x_0 = x_0(8:15);

% Add bits 8...15 from seed to cell
random_numbers{1} = x_0;

第2步:

for x = 2:n
    % Get previous number as base 10 number
    previous_number = bi2de(random_numbers{x - 1});

    % Calculate random number
    random_number = rem( (25173 * previous_number + 13849), 2^16 );

    % Convert random number to binary, extract bits 8...15
    random_number = de2bi(random_number, 16);
    random_number = random_number(8:15);

    % Save bits 8...15 of random number to cell
    random_numbers{x} = random_number;

    % Print bits 8...15 of random number as decimal number
    disp(num2str(bi2de(random_number)))
end

那是什么问题?如果运行此代码,您会注意到初始编号是唯一的.初始数字不会重复.但是,使用此算法创建的其余数字确实遵循一定的模式.我希望每个数字都遵循相同的模式.如果有人可以帮助我弄清楚我在做什么错,那将非常感谢.

So what's the problem? If you run this code you'll notice that the initial numbers are unique. The initial numbers do not repeat themselves. However the remaining numbers created with this algorithm do follow a certain pattern. I want every number to follow the same pattern. It would be very much appreciated if someone could help me figure out what I'm doing wrong.

推荐答案

您的代码看起来像经典的随机数生成算法,除了行

Your code looks like the classical algorithm for random number generation except for the line

random_number = random_number(8:15);

我假设您添加了该信息,因为您想生成0到255之间的随机数.问题是该行不仅影响输出数,还影响您保存用于下一个的 state 迭代.那就是可能导致问题的原因.

I assume you added that because you want to generate random numbers from 0 to 255. The problem is that that line is affecting not just the output number, but also the state you save for the next iteration. That's what's likely causing the problem.

如果您想更改输出数字,那很好,但是您不应该更改将用于生成下一个数字的状态.如果这样做,您将更改算法以及生成器状态序列的周期.

If you want to alter the output number, that's fine, but you shouldn't alter the state which will be used for generating the next number. If you do that you are changing the algorithm, and the period of the generator state sequence.

因此:将行random_number = random_number(8:15); 移到行random_numbers{x} = random_number;之后.

So: move the line random_number = random_number(8:15); after the line random_numbers{x} = random_number;.

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

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