Generate pseudorandom integers

Question:

In this project you will design and implement a class that can generate a sequence of pseudorandom integers, which is a sequence that appears random in many ways. The approach uses the linear congruence method, explained here.
The linear congruence method starts with a number called the seed. In addition to the seed, three other numbers are used in the linear congruence method, called the multiplier, the increment, and the modulus. The formula for generating a sequence of pseudorandom numbers is quite simple. The first number is: ( m u l t i p l i e r ∗ s e e d + i n c r e m e n t ) % m o d u l e s (multiplier*seed+increment)\%modules (multiplierseed+increment)%modulesThis formula uses the C++ % operator, which computes the remainder from an integer division.
Each time a new random number is computed, the value of the seed is changed to that new number. For example, we could implement a pseudorandom number generator with m u l t i p l i e r = 40 multiplier=40 multiplier=40, i n c r e m e n t = 725 increment=725 increment=725, and m o d u l e s = 729 modules=729 modules=729. If we choose the seed to be 1, then the sequence of numbers will proceed as shown here:
F i r s t n u m b e r First\enspace{number} Firstnumber = ( m u l t i p l i e r ∗ s e e d + i n c r e m e n t ) % m o d u l u s =(multiplier*seed+increment)\%modulus =(multiplierseed+increment)%modulus = ( 40 ∗ 1 + 725 ) % 729 =(40*1+725)\%729 =(401+725)%729 = 36 =36 =36and 36 becomes the new seed. N e x t n u m b e r Next\enspace{number} Nextnumber = ( m u l t i p l i e r ∗ s e e d + i n c r e m e n t ) % m o d u l u s =(multiplier*seed+increment)\%modulus =(multiplierseed+increment)%modulus = ( 40 ∗ 36 + 725 ) % 729 =(40*36+725)\%729 =(4036+725)%729 = 707 =707 =707and 707 becomes the new seed. N e x t n u m b e r Next\enspace{number} Nextnumber = ( m u l t i p l i e r ∗ s e e d + i n c r e m e n t ) % m o d u l u s =(multiplier*seed+increment)\%modulus =(multiplierseed+increment)%modulus = ( 40 ∗ 707 + 725 ) % 729 =(40*707+725)\%729 =(40707+725)%729 = 574 =574 =574and 574 becomes the new seed, and so on.
These particular values for multiplier, increment, and modulus happen to be good choices. The pattern generated will not repeat until 729 different numbers have been produced. Other choices for the constants might not be so good. For this project, design and implement a class that can generate a pseudorandom sequence in the manner described. The initial seed, multiplier, increment, and modulus should all be a member function to permit the seed to be changed, and a member function to generate and return the next number in the pseudorandom sequence.

My answer:

random.h

#ifndef RANDOM_H
#define RANDOM_H

class pseudorandom_generator{
public:
    pseudorandom_generator(int a, int b, int c, int d){
        multiplier = a;
        increment = b;
        modulus = c;
        seed = d;
    };
    void set_parameters(int a, int b, int c, int d);
    int generate_number();
private:
    int multiplier, seed, increment, modulus;
};

#endif

random.cpp

#include "random.h"
#include <iostream>

using namespace std;

void pseudorandom_generator::set_parameters(int a, int b, int c, int d){
    multiplier = a;
    increment = b;
    modulus = c;
    seed = d;
}

int pseudorandom_generator::generate_number(){
    seed = (multiplier*seed + increment)%modulus;
    cout << seed << " ";
    return seed;
}

main.cpp

#include "random.h"
#include <iostream>

using namespace std;

int main(){
    pseudorandom_generator a(40, 725, 729, 1);
    cout << "******start to generate pseudorandom numbers******" << endl;
    int i = 0;
    while(i < 729){
        a.generate_number();
        if((i+1)%20 == 0)
            cout << "\n";
        i++;
    }
    cout << "\n******here should be numbers range from 1 to 729 uniquely******" << endl;
    return 0;
}

结果:
picture
题中的生成伪随机数的方法叫做“线性同余法”。

Reference:

整理自 Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p123

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memories off

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值