# 线性同余法取随机数

#include <stdio.h>
#include <stdlib.h> //srand()、rand()
#include <time.h> //time();
int main() {
int n;
srand((unsigned)time(NULL)); //设置随机数种子
for (int i= 0; i< 10; i++) {
n = (rand() % 10) + 1 ;//产生1~10的随机数
//rand()产生的是一个很大的数，对其求余就可以达到限定范围的目的
printf("%d ", n);
}
return 0;
}


seed = (a*seed) mod m;
or
seed = (a*seed + c) mod m;


a, m为正整数，m> a, m> seed;

Schrage’s Method Revealed算法，将公式形式变换，避免了溢出。

x = (a*x) mod m
= a*(x mod Q) - R*[xi/Q];
if (x > 0) result_seed = x;
else result_seed = x + m;

random_my.h文件

namespace RAND_GEN {
class mod_my {
public:
long long m, a, c;
mod_my(long long _m, long long _a, long long _c) : m(_m), a(_a), c(_c) {}

//  General case for x = (ax + c) mod m -- use Schrage's algorithm
//  to avoid integer overflow.
//  (ax + c) mod m can be rewritten as:
//    a(x mod q) - r(x / q)  if >= 0
//    a(x mod q) - r(x / q)  otherwise
//  where: q = m / a , r = m mod a
//
//  Preconditions:  a > 0, m > 0.
//
//  Note: only works correctly for m % a < m / a.
long long calc(long long x) {
if (a == 1) {
x %= m;
} else {
long long q = m / a;
long long r = m % a;
long long t1 = a * (x % q);
long long t2 = r * (x / q);
if (t1 >= t2) x = t1 - t2;
else x = m - t2 + t1;
}
if (c != 0) {
const long long d = m - x;
if (d > c) x += c;
else x = c - d;
}
return x;
}
};

class linear_congruential_engine_my {
public:
long long multiplier, increment, modulus;
unsigned long long default_seed_my, seed_my;
mod_my mod_temp;

linear_congruential_engine_my()
: multiplier(16807), increment(1), modulus(2147483647)
, default_seed_my(1u), mod_temp(modulus, multiplier, increment)
{ seed(default_seed_my); }

linear_congruential_engine_my(long long _m, long long _a,
long long _c, long long _s)
: multiplier(_a), increment(_c), modulus(_m)
, default_seed_my(_s), mod_temp(modulus, multiplier, increment)
{ seed(default_seed_my); }

void seed(unsigned long long s)
{ seed_my = s; }

long long min()
{ return  increment == 0u ? 1u : 0u; }

long long max()
{ return modulus - 1u; }

{ for (; z != 0ULL; --z) (*this)(); }

long long operator()() {
seed_my = mod_temp.calc(seed_my);
return seed_my;
}
};

}
main.cpp

#include <iostream>
#include "random_my.h"
using namespace std;
using namespace RAND_GEN;

void test_calc() {
mod_my mod_1(9223372036854775807, 16807, 1);
if (mod_1.calc(9223372036854775) != 7443261233741790514 ||
mod_1.calc(922337203685477580) != 6456360425798331301 ||
mod_1.calc(9223372036852222220) != 9223371993936639099 ||
mod_1.calc(922337203685473330) != 6456360425726901551 ||
mod_1.calc(9223372022254775806) != 9223126654654759001)
cout << "Your calc() is wrong.\n";
else cout << "Pass all tests for calc().\n";
}

void test_engin() {
linear_congruential_engine_my lce;
int count = 1000;
int num[1001] = {0};
while (count--) num[lce()%5]++;
if (num[0] != 216 || num[1] != 190 ||
num[2] != 203 || num[3] != 216 ||
num[4] != 175) {
cout << "Your engin class is wrong in generator.\n";
return;
} else if (lce.min() != (lce.increment == 0u ? 1u : 0u)) {
cout << "Your engin class is wrong in min().\n";
return;
} else if (lce.max() != (lce.modulus - 1u)) {
cout << "Your engin class is wrong in max().\n";
return;
}
else cout << "Pass all tests for class engin.\n";
}

void hard_test() {
long long m, a, c, n, num[5] = {0};
unsigned long long s;
cin >> m >> a >> c >> s;
mod_my mod_1(m, a, c);
for (int i = 0; i < 5; i++) {
cin >> n;
cout << "(MOD CALC) ";
cout << n << ": " << mod_1.calc(n) << endl;
}
linear_congruential_engine_my lce(m, a, c, s);
cin >> n;
while (n--) num[lce()%5]++;
for (int i = 0; i < 5; i++) {
cout << "(ENGIN) ";
cout << i << ": " << num[i] << endl;
}
}

int main() {
int t;
cin >> t;
if (t == 0) {
test_calc();
test_engin();
} else {
hard_test();
}
return 0;
}

05-02 269

10-10 9402

09-07 76

11-03 21

#### 团购码，你知多少？——线性同余产生随机数

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

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