随机化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 ...
随机推荐
- KDE算法解析
核密度估计(Kernel Density Estimation, KDE)算法通过样本估计这些样本所属的概率密度函数,是non-parametric方法,也就是在进行估计时无需假设分布的具体形式.本文 ...
- KingbaseES V8R6 集群运维系列--archive_cleanup_command参数应用
案例说明: 参数archive_cleanup_command可以配置在kingbase.conf文件中,用于备库清理不在需要的归档日志,参数详细说明见下图: https://postgresql ...
- KingabseES例程-函数和过程的 INVOKER 与 DEFINER
调用者权利和定义者权利子句 指定子程序的权利属性.权利属性影响单元在运行时,执行的SQL语句的名称解析和权限检查. PG模式: SECURITY INVOKER SECURITY DEFINER Or ...
- Scala 特质自身类型
1 package chapter06 2 3 object Test16_TraitSelfType { 4 def main(args: Array[String]): Unit = { 5 va ...
- SqlServer的实用且高级玩法.md
1.常见表表达式(CTEs) 如果您想要查询子查询,那就是CTEs施展身手的时候 - CTEs基本上创建了一个临时表. 使用常用表表达式(CTEs)是模块化和分解代码的好方法,与您将文章分解为几个段落 ...
- Ubuntu一键安装/卸载docker和docker compose,可指定版本或安装最新版本。
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 @ 目录 前言 一.docker是什么? 二.docker compose是什么? 三.安装步骤 1.Ubuntu安装脚本 2.生成脚 ...
- #最大密度子图,0/1分数规划#UVA1389 Hard Life
题目 \(n\) 个点,\(m\) 条边的一个无向图,问导出子图的边数除以点数的最大值 分析 考虑二分这个答案,也就是0/1分数规划之后转换成 \(E-mid*V>0\) 这个问题虽然可以精确到 ...
- #斐波那契#洛谷 3424 [POI2005] SUM-Fibonacci Sums
题目 已知\(x,y\)的斐波那契表示,求\(x+y\)的斐波那契表示 分析 显然得到两条性质: \(f_{i+1}=f_{i-1}+f_i\) \(2f_i=f_{i+1}+f_{i-2}\) 那么 ...
- Node 项目通过 .npmrc 文件指定依赖安装源
背景 npm 命令运行时,往往通过命令行指定相关配置,最常用的便是使用 --registry 来指定依赖的安装源. npm install --registry=https://registry.np ...
- OpenHarmony携千行百业创新成果亮相HDC.Together 2023
8月4日-6日,华为开发者大会2023(以下简称"大会")在中国松山湖举办,OpenAtom OpenHarmony(简称"OpenHarmony")隆重参会 ...