随机化Tricks
参阅:
https://zh.cppreference.com/w/cpp/numeric/random
https://zh.cppreference.com/w/cpp/header/random
- 使用
random_device[1]作为种子
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的常用成员函数
构造函数:
mt19937():默认构造函数,使用默认的种子初始化随机数引擎。mt19937(unsigned int seed):使用指定的种子初始化随机数引擎。
种子操作函数:
seed():设置种子值为默认值。seed(unsigned int seed):设置新的种子值。
随机数生成函数:
operator():生成一个32位的随机整数。
辅助函数:
discard(unsigned long long z):等同于执行z次operator(),以丢弃z次生成的随机数。min():获取可生成的最小随机数值。max():获取可生成的最大随机数值。
随机化Tricks的更多相关文章
- 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 ...
- APP漏洞扫描用地址空间随机化
APP漏洞扫描用地址空间随机化 前言 我们在前文<APP漏洞扫描器之本地拒绝服务检测详解>了解到阿里聚安全漏洞扫描器有一项静态分析加动态模糊测试的方法来检测的功能,并详细的介绍了它在针对本 ...
- testng 教程之使用参数的一些tricks配合使用reportng
前两次的总结:testng annotation生命周期 http://www.cnblogs.com/tobecrazy/p/4579414.html testng.xml的使用和基本配置http: ...
- (转) 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 ...
- Matlab tips and tricks
matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it g ...
- rabin 素性检验 随机化算法
#include <cstdio> #include <cstdlib> #include <ctime> typedef long long int LL; in ...
- LoadRunner AJAX TruClient协议Tips and Tricks
LoadRunner AJAX TruClient协议Tips and Trickshttp://automationqa.com/forum.php?mod=viewthread&tid=2 ...
- 【翻译】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 ...
- [USACO2005][POJ2454]Jersey Politics(随机化)
题目:http://poj.org/problem?id=2454 题意:给你3*k(k<=60)个数,你要将它们分成3个长度为k的序列,使得其中至少有两个序列的和大于k*500 分析:以为有高 ...
- 神经网络训练中的Tricks之高效BP(反向传播算法)
神经网络训练中的Tricks之高效BP(反向传播算法) 神经网络训练中的Tricks之高效BP(反向传播算法) zouxy09@qq.com http://blog.csdn.net/zouxy09 ...
随机推荐
- vue3中使用simple-keyboard实现虚拟键盘(带中文切换数字键盘)
效果图 官网 simple-keyboard官网:https://hodgef.com/simple-keyboard/ 打不开的话请用魔法 不足 中文语言包支持度不够.不过自己可以找语言包替换 依赖 ...
- node-xlsx处理excel
引用node-xlsx npm install node-xlsx -save 文件中引用 const xlsx = require('node-xlsx') 读取excel const sheet ...
- archlinux xfce未中文化 goldendict不能显示中文
下载个中文字体包就好了 https://wiki.archlinuxcn.org/wiki/简体中文本地化
- wordpress搭建个人博客(阿里云服务器、宝塔面板、Elementor)
wordpress搭建个人博客(阿里云服务器.宝塔面板.Elementor) 笔记根据bilibili比得汪视频教程整理 https://www.bilibili.com/video/BV1Vg411 ...
- Java 实现OCR扫描/识别图片文字
图片内容一般无法编辑,如果想要读取图片中的文本,我们需要用到OCR工具.本文将介绍如何在Java中实现OCR识别读取图片中的文字. 所需工具: IDEA Spire.OCR for Java - Ja ...
- ET介绍——Actor Location
Actor Location Actor模型只需要知道对方的InstanceId就能发送消息,十分方便,但是有时候我们可能无法知道对方的InstanceId,或者是一个Actor的InstanceId ...
- #贪心#CF840A Leha and Function
题目 设 \(f(n,k)\) 表示 区间 \([1,n]\) 选出 \(k\) 个元素的集合的期望最小值, 现在需要重排 \(a\) 数组,使得 \(\sum_{i=1}^mf(a_i,b_i)\) ...
- Docker学习路线6:使用第三方镜像
第三方镜像是在Docker Hub或其他容器注册表上提供的预构建Docker容器镜像.这些镜像由个人或组织创建和维护,可以作为您容器化应用程序的起点. 查找第三方镜像 Docker Hub 是最大和最 ...
- Hessian反序列化分析
RPC协议 RPC全称为Remote Procedure Call Protocol(远程调用协议),RPC和之前学的RMI十分类似,都是远程调用服务,它们不同之处就是RPC是通过标准的二进制格式来定 ...
- input 去除默认样式
前言 如何不自己写框架,基本用不上. 正文 input{ border: 0px; background-color: none; outline: none; } input:focus{ outl ...