随机化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 ...
随机推荐
- IDEA 突然无法打开 解决办法
如果实在没有方法了可以试一试 删除所有的配置文件,默认位置C:\Users\${你的用户名}\${.IntelliJIdea+版本} 注意:删除配置后之前所有的设置都会失效,谨慎操作.
- C# 二维码生成、识别,去除白边、任意颜色
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 基于SCCB协议的FPGA实现
SCCB协议 1.协议内容 SCCB协议常用于vo系列的摄像头的寄存器配置中,是有IIC协议演变而来.本来,本人接触这个协议也是想配置摄像头用于摄像模块.但是,由于配置寄存器实在是太多,而且需要找的资 ...
- KingbaseES V8R6集群运维系列 -- 修改ssh通信为 sys_securecmdd 通信
一.适用于: 本文档使用于KingbaseES V008R006版本. 二.关于SYS_SECURECMDD: sys_securecmdd是KingbaseES集群自带的工具,集群监控.管理集群时通 ...
- 无监督学习-K-means算法
无监督学习-K-means算法 1. 什么是无监督学习 一家广告平台需要根据相似的人口学特征和购买习惯将美国人口分成不同的小组,以便广告客户可以通过有关联的广告接触到他们的目标客户. Airbnb 需 ...
- Java 实现OCR扫描/识别图片文字
图片内容一般无法编辑,如果想要读取图片中的文本,我们需要用到OCR工具.本文将介绍如何在Java中实现OCR识别读取图片中的文字. 所需工具: IDEA Spire.OCR for Java - Ja ...
- Codeforces Round #682 (Div. 2)
CF1438A Specific Tastes of Andre 洛谷传送门 CF1438A 代码(全铺成1就可以了) #include <cstdio> #include <cct ...
- OpenHarmony应用HAP包签名
背景 OpenAtom OpenHarmony(以下简称"OpenHarmony")应用如果需要对外发布就必须要通过应用签名,DevEco Studio已提供自动签名功能加速应用开 ...
- 【直播回顾】OpenHarmony知识赋能六期第三课—OpenHarmony智能家居项目之控制面板功能实现
7月14日晚上19点,知识赋能第六期第三节直播 <OpenHarmony智能家居项目之控制面板功能实现> ,在OpenHarmony开发者成长计划社群内成功举行. 本次直播是"O ...
- Numpy结构化数组
Numpy结构化数组 Numpy的结构化数组和记录数组为复合的.异构的的数据提供了非常有效的存储. 结构化数组 In [1]: import numpy as np In [2]: name = [' ...