参阅:

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. java 计算两个日期相差工作日天数

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.DayOfWeek; impo ...

  2. Oracle 视图创建自增ID

    select ROW_NUMBER() over (order by sysdate()) as id

  3. 自建AXI4的IP核的地址问题

    自建AXI4的IP核的地址问题 1.问题概述 在AXI4中,对应的操作通过对对应的寄存器设置来完成.其中的寄存器又分为数据寄存器和地址寄存器.其中,如果只是想要操作数据的话,只需要使用数据寄存器即可. ...

  4. Python日志模块封装

    一.先上结论 对Python logging模块进行二次封装 # -*- coding:utf-8 -*- # 作者:IT小学生蔡坨坨 # 博客:caituotuo.top # 时间:2022/3/1 ...

  5. MySQL 索引失效场景总结

    查询条件有 or 假设在 customer_name 字段设置了普通索引,执行以下 sql: # type: ref, possible_keys: idx_customer_name, key: i ...

  6. 【Java面试题】Mybatis

    五.MyBatis 40)谈谈 MyBatis Mybatis 是一个半自动化的 ORM 框架,它对 jdbc 的操作数据库的过程进行封装,使得开发者只需要专注于 SQL 语句本身,而不用去关心注册驱 ...

  7. 使用POI、JavaCsv工具读取excel文件(*.xls , *.xlsx , *.csv)存入MySQL数据库

    首先进行maven的配置:导入相关依赖 1 <dependency> 2 <groupId>org.apache.poi</groupId> 3 <artif ...

  8. 区别对比 Python、Perl、VB、Ruby、C/C++、C# .Net、PHP、Java… 等多编程语言

    1. 编程语言类型:首先,Python.C/C++.Java 都是强类型的编程语言.强类型的编程语言定义如下: 强类型的编程语言是一种强制类型定义的编程语言,即一旦某一个变量被定义类型,如果不经强制转 ...

  9. 【FAQ】HarmonyOS SDK 闭源开放能力 —Push Kit

    1.问题描述 升级到4.0.0.59版本后,通过pushService.getToken获取华为的token时报如下错误:Illegal application identity. 解决方案 Mate ...

  10. Python读写json文件--json

    import json # 将数据写入json文件 def json_write_file(): data={'name':'张三','age':12} with open('json.json',' ...