参阅:

https://zh.cppreference.com/w/cpp/numeric/random

https://zh.cppreference.com/w/cpp/header/random


  1. 使用random_device[1]作为种子
  1. 新建mt19937随机数

  2. uniform_int_distribution[2](整数范围)、uniform_real_distribution[3](实数范围)限定范围

Code:

// C++11
#include<cstdio>
#include<random>
#include<chrono>
using namespace std;
#define clock() (chrono::steady_clock::now())
const int n=1e7;
int a[11];
int main() {
mt19937 rnd(random_device{}());
uniform_int_distribution<int> int_dist(1,10); // 整数
uniform_real_distribution<double> real_dist(1,10); // 实数
printf("%d %lf\n",int_dist(rnd),real_dist(rnd));
auto st=clock();
for(int i=1; i<=n; ++i)
++a[int_dist(rnd)];
printf("int_dist costed time: %.3lfs\n",chrono::duration<double>(clock()-st).count());
for(int i=1; i<=10; ++i)
printf("%d%c",a[i]," \n"[i==10]);
st=clock();
for(int i=1; i<=n; ++i)
real_dist(rnd);
printf("real_dist costed time: %.3lfs\n",chrono::duration<double>(clock()-st).count());
return 0;
}

A possible output:

7 6.686022
int_dist costed time: 0.130s
1001752 997709 1000564 1000381 999916 997915 1000190 999793 999096 1002684
real_dist costed time: 0.270s

附:mt19937的常用成员函数

  1. 构造函数:

    • mt19937():默认构造函数,使用默认的种子初始化随机数引擎。

    • mt19937(unsigned int seed):使用指定的种子初始化随机数引擎。

  2. 种子操作函数:

    • seed():设置种子值为默认值。

    • seed(unsigned int seed):设置新的种子值。

  3. 随机数生成函数:

    • operator():生成一个32位的随机整数。
  4. 辅助函数:

    • discard(unsigned long long z):等同于执行z次operator(),以丢弃z次生成的随机数。

    • min():获取可生成的最小随机数值。

    • max():获取可生成的最大随机数值。


  1. https://zh.cppreference.com/w/cpp/numeric/random/random_device

  2. https://zh.cppreference.com/w/cpp/numeric/random/uniform_real_distribution

  3. https://zh.cppreference.com/w/cpp/numeric/random/uniform_int_distribution

随机化Tricks的更多相关文章

  1. Matlab Tricks(二十六)—— 置乱(随机化)与恢复(shuffle/permutation & restore)

    x = 1:10; n = length(x); perm = randperm(n); x_perm = x(perm); % x_perm 表示置乱后的结果 x_ori(perm) = x_per ...

  2. APP漏洞扫描用地址空间随机化

    APP漏洞扫描用地址空间随机化 前言 我们在前文<APP漏洞扫描器之本地拒绝服务检测详解>了解到阿里聚安全漏洞扫描器有一项静态分析加动态模糊测试的方法来检测的功能,并详细的介绍了它在针对本 ...

  3. testng 教程之使用参数的一些tricks配合使用reportng

    前两次的总结:testng annotation生命周期 http://www.cnblogs.com/tobecrazy/p/4579414.html testng.xml的使用和基本配置http: ...

  4. (转) How to Train a GAN? Tips and tricks to make GANs work

    How to Train a GAN? Tips and tricks to make GANs work 转自:https://github.com/soumith/ganhacks While r ...

  5. Matlab tips and tricks

    matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it g ...

  6. rabin 素性检验 随机化算法

    #include <cstdio> #include <cstdlib> #include <ctime> typedef long long int LL; in ...

  7. LoadRunner AJAX TruClient协议Tips and Tricks

    LoadRunner AJAX TruClient协议Tips and Trickshttp://automationqa.com/forum.php?mod=viewthread&tid=2 ...

  8. 【翻译】C# Tips & Tricks: Weak References - When and How to Use Them

    原文:C# Tips & Tricks: Weak References - When and How to Use Them Sometimes you have an object whi ...

  9. [USACO2005][POJ2454]Jersey Politics(随机化)

    题目:http://poj.org/problem?id=2454 题意:给你3*k(k<=60)个数,你要将它们分成3个长度为k的序列,使得其中至少有两个序列的和大于k*500 分析:以为有高 ...

  10. 神经网络训练中的Tricks之高效BP(反向传播算法)

    神经网络训练中的Tricks之高效BP(反向传播算法) 神经网络训练中的Tricks之高效BP(反向传播算法) zouxy09@qq.com http://blog.csdn.net/zouxy09 ...

随机推荐

  1. Vue3项目-生成Cron表达式组件

    最近做的一个vue3项目过程中,需要用到cron表达式功能,而对于普通业务人员,他们是不懂cron表达式规则的,所以需要做一个可手动配置生成cron表达式的功能.从网上查找了一些相关资料,然后结合vu ...

  2. JDK8 ::用法(双冒号)

    JDK8中有双冒号的用法,就是把方法当做参数传到stream内部,使stream的每个元素都传入到该方法里面执行一下. List<String> lt = Arrays.asList(&q ...

  3. KingbaseES 用户密码认证及加密算法

    kingbaseES用户的口令被存储在sys_authid系统表中. 口令可以用SQL命令create user 和alter user 管理,例如 :create/alter user u1 wit ...

  4. JavaScript二代公民身份证号验证

    身份证号码中的校验码是身份证号码的最后一位,是根据[中华人民共和国国家标准GB 11643-1999]中有关公民身份号码的规定, 根据精密的计算公式计算出来的,公民身份号码是特征组合码,由十七位数字本 ...

  5. #原根,BSGS,扩欧,矩阵乘法#CF1106F Lunar New Year and a Recursive Sequence

    题目 已知数列 \(f\) 满足 \(f_{1\sim k-1}=1\) 且 \(f_n=m\), 并且知道 \(f_i=(\prod_{j=1}^kf_{i-j}b_j)\bmod{99824435 ...

  6. 使用脚本整合指定文件/文件夹,执行定制化 ESLint 命令

    背景 最近面对一个庞大的项目,但是只需要修改某个模块,每次都手搓命令太麻烦了,于是就想着能不能写个脚本来辅助处理这些事情. 解决方案 定制化一键 ESLint,执行文件下载地址: https://gi ...

  7. Jetty使用入门

    社区当前推荐开发者使用Jetty 12.X版本. 依据End of Community Support for Jetty 9.x - June 2022,社区对Jetty 9.x的支持,已在2022 ...

  8. 80+产品正通过兼容性测试,OpenHarmony生态蓬勃发展

    4 月 25 日,开放原子开源基金会举办了 OpenAtom OpenHarmony(以下简称"OpenHarmony")技术日活动,OpenHarmony PMC 委员代表首次对 ...

  9. 3. Vector Spaces and Subspaces

    3.1 Vector Spaces The space \(R^n\) consists of all colunm vectors \(v\) with n components. We can a ...

  10. Godot Label样式 Textrue纹理,实现样式修改,背景填充

    目录 前言 运行环境 新建项目 Style 样式讲解 StyleBoxEmpty:普通样式 StyleBoxTexture:字体样式 StyleBoxFlat:填充样式 StyleBoxLine:行样 ...