说明,代码摘取自webrtc中的代码库,随机数生成算法:
random.h
#ifndef __RANDOM_H__
#define __RANDOM_H__
#include<limits>
#include<stdint.h>
#include<math.h>
class Random {
public:
// TODO(tommi): Change this so that the seed can be initialized internally,
// e.g. by offering two ways of constructing or offer a static method that
// returns a seed that's suitable for initialization.
// The problem now is that callers are calling clock_->TimeInMicroseconds()
// which calls TickTime::Now().Ticks(), which can return a very low value on
// Mac and can result in a seed of 0 after conversion to microseconds.
// Besides the quality of the random seed being poor, this also requires
// the client to take on extra dependencies to generate a seed.
// If we go for a static seed generator in Random, we can use something from
// webrtc/base and make sure that it works the same way across platforms.
// See also discussion here: https://codereview.webrtc.org/1623543002/
explicit Random(uint64_t seed);
// Return pseudo-random integer of the specified type.
// We need to limit the size to 32 bits to keep the output close to uniform.
template <typename T>
T Rand() {
static_assert(std::numeric_limits<T>::is_integer &&
std::numeric_limits<T>::radix == 2 &&
std::numeric_limits<T>::digits <= 32,
"Rand is only supported for built-in integer types that are "
"32 bits or smaller.");
return static_cast<T>(NextOutput());
}
// Uniformly distributed pseudo-random number in the interval [0, t].
uint32_t Rand(uint32_t t);
// Uniformly distributed pseudo-random number in the interval [low, high].
uint32_t Rand(uint32_t low, uint32_t high);
// Uniformly distributed pseudo-random number in the interval [low, high].
int32_t Rand(int32_t low, int32_t high);
// Normal Distribution.
double Gaussian(double mean, double standard_deviation);
// Exponential Distribution.
double Exponential(double lambda);
private:
// Outputs a nonzero 64-bit random number.
uint64_t NextOutput() {
state_ ^= state_ >> 12;
state_ ^= state_ << 25;
state_ ^= state_ >> 27;
//RTC_DCHECK(state_ != 0x0ULL);
return state_ * 2685821657736338717ull;
}
uint64_t state_;
RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Random);
};
// Return pseudo-random number in the interval [0.0, 1.0).
template <>
float Random::Rand<float>();
// Return pseudo-random number in the interval [0.0, 1.0).
template <>
double Random::Rand<double>();
// Return pseudo-random boolean value.
template <>
bool Random::Rand<bool>();
#endif // __RANDOM_H__
random.cc
#include"random.h"
Random::Random(uint64_t seed) {
//RTC_DCHECK(seed != 0x0ull);
state_ = seed;
}
uint32_t Random::Rand(uint32_t t) {
// Casting the output to 32 bits will give an almost uniform number.
// Pr[x=0] = (2^32-1) / (2^64-1)
// Pr[x=k] = 2^32 / (2^64-1) for k!=0
// Uniform would be Pr[x=k] = 2^32 / 2^64 for all 32-bit integers k.
uint32_t x = NextOutput();
// If x / 2^32 is uniform on [0,1), then x / 2^32 * (t+1) is uniform on
// the interval [0,t+1), so the integer part is uniform on [0,t].
uint64_t result = x * (static_cast<uint64_t>(t) + 1);
result >>= 32;
return result;
}
uint32_t Random::Rand(uint32_t low, uint32_t high) {
//RTC_DCHECK(low <= high);
return Rand(high - low) + low;
}
int32_t Random::Rand(int32_t low, int32_t high) {
//RTC_DCHECK(low <= high);
// We rely on subtraction (and addition) to be the same for signed and
// unsigned numbers in two-complement representation. Thus, although
// high - low might be negative as an int, it is the correct difference
// when interpreted as an unsigned.
return Rand(high - low) + low;
}
template <>
float Random::Rand<float>() {
double result = NextOutput() - 1;
result = result / 0xFFFFFFFFFFFFFFFEull;
return static_cast<float>(result);
}
template <>
double Random::Rand<double>() {
double result = NextOutput() - 1;
result = result / 0xFFFFFFFFFFFFFFFEull;
return result;
}
template <>
bool Random::Rand<bool>() {
return Rand(0, 1) == 1;
}
double Random::Gaussian(double mean, double standard_deviation) {
// Creating a Normal distribution variable from two independent uniform
// variables based on the Box-Muller transform, which is defined on the
// interval (0, 1]. Note that we rely on NextOutput to generate integers
// in the range [1, 2^64-1]. Normally this behavior is a bit frustrating,
// but here it is exactly what we need.
const double kPi = 3.14159265358979323846;
double u1 = static_cast<double>(NextOutput()) / 0xFFFFFFFFFFFFFFFFull;
double u2 = static_cast<double>(NextOutput()) / 0xFFFFFFFFFFFFFFFFull;
return mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2);
}
double Random::Exponential(double lambda) {
double uniform = Rand<double>();
return -log(uniform) / lambda;
}
真随机数的获取在linux可以对设备/dev/urandom进行读取,在windows下则可以调用RtlGenRandom。
还可以采用mt19937算法生成。
C++代码如下:
#include <random>
#include <iostream>
struct MT19937 {
private:
static std::mt19937_64 rng;
public:
// This is equivalent to srand().
static void seed(uint64_t new_seed = std::mt19937_64::default_seed) {
rng.seed(new_seed);
}
// This is equivalent to rand().
static uint64_t get() {
return rng();
}
static double get_double()
{
double result=rng();
return result/std::numeric_limits<uint64_t>::max();
}
};
std::mt19937_64 MT19937::rng;
int main() {
MT19937::seed(/*put your seed here*/);
for (int i = 0; i < 10; ++ i)
std::cout << MT19937::get() << std::endl;
}
文章浏览阅读963次,点赞26次,收藏10次。【C++学习】map和set
文章浏览阅读1.3k次。阅读本篇文章,您将可以了解:1、什么是SaaS;2、SaaS的商业模式;3、SaaS的技术架构;4、国内比较好的SaaS平台。_saas如何开发
文章浏览阅读1.5k次。摄像头接入目前摄像头直播的方案主要有以下几种方式: rtsp方式接入,只能实现视频预览 国标协议接入,实现比较复杂,需要多实现SIP服务器 通过netsdk获取到视频码流,推流到流媒体服务器,通过wsflv,flv,hls等流媒体协议播放,H265不支持 一、采用方案对比后最终采用了第三种方式,java使用jna的方式接入大华netsdk,获取到dav视频码流后去除大华头尾,拿到H264裸码流,通过javacv(对ffmpeg、opencv等库的封装)推送到.._大华sdk frealdatacallback码流输出链接
文章浏览阅读1.4k次。因为改动比较小,所以我不想重建一个commit,于是我是用了git commit --amend命令,由于之前已经将该commit推送到远程仓库,导致修改后推送失败。百度后发现如果你的commit已经push到了远程仓库,那么使用--amend修改commit后,git push时一定要使用 --force-with-lease 参数来强制推送,否则就会报错。这是我自己推送失败的例子解决方式一、第一种使用后git commit --amend -m "修改Git学习(三)指令"注意:-m “._git remote-tracking 本地commit amend不了
文章浏览阅读2.5k次,点赞6次,收藏15次。AI实验1——八数码问题一、实验目的与要求实验目的:1 . 熟悉状态空间表示法;2.掌握深度优先、广度优先和等代价无信息搜索算法;3.掌握启发式函数设计,实现面向实际问题的A*搜索算法;二、实验内容与方法实验内容:利用无信息搜索算法实现八数码难题求解;设计启发式信息函数,利用A*搜索实现八数码难题求解;三、实验步骤与过程1,问题分析在八数码难题中,我们使用状态空间表示法,将八数码矩阵(即矩阵的状态)设置为一个节点类(Node),各个节点之间通过操作集(Operater)[‘U_人工智能实验八数码难题
文章浏览阅读2.8k次,点赞4次,收藏11次。透明背景转jpg格式后变黑我们先看demo,您可以狠狠地点击这里:png图片是否含有透明像素JS检测demo如果是不含透明色的PNG图片,则会提示不含透明;如果是,则提示含透明,如下截图:是否背景透明的检测检测原理是借助canvas的getImageData()方法,关于此方法具体API和使用,可以参见““像素点信息获取”这里的详细介绍。_js-demo抠图
文章浏览阅读275次。用Java语言实现Windows版本的记事本程序(实现了部分功能模块的记事本),代码复制粘贴如下:import java.awt.;import java.io.;import java.awt.event.;import javax.swing.;import java.net.;import javax.swing.border.;import java.util.Date;import java.text.SimpleDateFormat;import java.lang.*;_记事本 cambria math 分数
文章浏览阅读6k次,点赞8次,收藏55次。对于Linux操作系统来说,一般通过VNC、Teamviewer和SSH等工具来进行远程管理,SSH是 Secure Shell的缩写,由IETF的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和加粗样式其他网络服务提供安全性的协议。利用SSH协议可以有效防止远程管理过程中的信息泄露问题。SSH客户端适..._password top 1000
文章浏览阅读1.2k次。这篇文章主要为大家详细介绍了【荐】静态页面实现微信分享带缩略图、标题和描述,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴。静态页面实现微信分享带缩略图、标题和描述,想法很现实,要想实现这样的功能这里就要用到大ajax技术,在这里361源码分享给大家。服务端文件jssdk.php代码:..._html分享链接加图片和描述不需要调用jssdk
文章浏览阅读5.9k次,点赞10次,收藏63次。大师兄科研实例使用方法:1从最基本的计算开始,通过示例讲解,结合一些脚本的使用,引导大家思考解决自己的问题。因此,在这本书的学习过程里,每一章节会对应一个例子,大家务必手动搭建模型,输入文件(切忌复制粘贴),然后进行计算,得到和大师兄一致的结果。为了引导大家主动浏览官网解决问题,很多都会采用VASP官网的例子。2如何学习本书,大师兄在学习程序时,受learn_python_the_hard_w..._vaspwiki
文章浏览阅读222次。以前觉得深圳好热,可是现在觉得沈阳的风好冷…每天不需要说太多的话,只要敲着喜欢的键盘,默默的清理掉禅道上的bug就好,这样的工作似乎是充实的,却总是少点什么,我也喜欢上了活在自己的小世界里,喜欢上了听歌,喜欢去刷微博,喜欢上去看那些心灵鸡汤和搞笑的博主,喜欢看看最近的陈伟霆是不是有帅了,偶尔碰到一条好玩的微博似乎会笑上一会儿,可能作为一个程序猿,真的比较枯燥叭,有时觉得孤单的像一条_好多好多话
文章浏览阅读2.4k次。计算机硬件维护的四大原则近年来,随着社会经济的快速发展和计算机网络技术的广泛应用,如今计算机已经成为生产生活中不可或缺的必需品,随之而来就出现一些问题。下面是YJBYS小编为大家搜索整理的关于计算机硬件维护的四大原则,欢迎参考阅读,希望对大家有所帮助!想了解更多相关信息请持续关注我们应届毕业生培训网!对于计算机而言,主要包括硬件系统与软件系统两部分,其中硬件系统是软件系统功能得以实现的重要基础和前..._硬件维修的基本原则?