虽然随机引擎需要在每个编译器上给出相同的数字序列。至少有一些随机分布不是,只要求它们满足统计和概率阈值。例如:
#include <random>
#include <iostream>
int main() {
std::mt19937 foo;
std::uniform_int_distribution<int> bar(0, 1000);
for (int i=0; i<99; ++i) {
bar(foo);
}
std::cout << bar(foo) << std::endl;
return 0;
}
针对(我的版本)libstdc++ 编译时将打印 808,针对 libc++ 编译时将打印 89。
无论给定什么样的合规环境,哪个标准提供的分布函数(如果有的话)都能保证产生一致的结果?
最佳答案
不幸的是,从 N3936(C++14 最终草案)开始,没有提供随机分布的标准有这样的要求。很容易看出原因。有许多编写分布函数的有效方法。有些比其他的好。甚至像正态分布这样基本的东西的算法也越来越好,并且是积极研究的主题。强制使用单个算法会不必要地阻碍 future 算法的实现。
幸运的是,您可以自己编写。各种分发类的 header 规范位于 §26.5.8 下。但是,您没有理由必须遵循这种结构。
(请注意,我没有彻底测试过这段代码,某些引擎可能会出现不良行为,或者溢出,尽管我已经付出了一些努力来避免后者,这更多的是作为一个说明性的例子,而不是一个令人敬畏的均匀分布的规范来源。话虽这么说,如果你发现它有任何问题,请在评论中告诉我,我很乐意纠正它。)
#include <random>
#include <tuple>
#include <iostream>
template<class IntType = int>
class my_uniform_int_distribution {
public:
// types
typedef IntType result_type;
typedef std::pair<int, int> param_type;
// constructors and reset functions
explicit my_uniform_int_distribution(IntType a = 0, IntType b = std::numeric_limits<IntType>::max());
explicit my_uniform_int_distribution(const param_type& parm);
void reset();
// generating functions
template<class URNG>
result_type operator()(URNG& g);
template<class URNG>
result_type operator()(URNG& g, const param_type& parm);
// property functions
result_type a() const;
result_type b() const;
param_type param() const;
void param(const param_type& parm);
result_type min() const;
result_type max() const;
private:
typedef typename std::make_unsigned<IntType>::type diff_type;
IntType lower;
IntType upper;
};
template<class IntType>
my_uniform_int_distribution<IntType>::my_uniform_int_distribution(IntType a, IntType b) {
param({a, b});
}
template<class IntType>
my_uniform_int_distribution<IntType>::my_uniform_int_distribution(const param_type& parm) {
param(parm);
}
template<class IntType>
void my_uniform_int_distribution<IntType>::reset() {}
template<class IntType>
template<class URNG>
auto my_uniform_int_distribution<IntType>::operator()(URNG& g) -> result_type {
return operator()(g, param());
}
template<class IntType>
template<class URNG>
auto my_uniform_int_distribution<IntType>::operator()(URNG& g, const param_type& parm) -> result_type {
diff_type diff = (diff_type)parm.second - (diff_type)parm.first + 1;
if (diff == 0) // If the +1 overflows we are using the full range, just return g()
return g();
diff_type badDistLimit = std::numeric_limits<diff_type>::max() / diff;
do {
diff_type generatedRand = g();
if (generatedRand / diff < badDistLimit)
return (IntType)((generatedRand % diff) + (diff_type)parm.first);
} while (true);
}
template<class IntType>
auto my_uniform_int_distribution<IntType>::a() const -> result_type {
return lower;
}
template<class IntType>
auto my_uniform_int_distribution<IntType>::b() const -> result_type {
return upper;
}
template<class IntType>
auto my_uniform_int_distribution<IntType>::param() const -> param_type {
return {lower, upper};
}
template<class IntType>
void my_uniform_int_distribution<IntType>::param(const param_type& parm) {
std::tie(lower, upper) = parm;
if (upper < lower)
throw std::exception();
}
template<class IntType>
auto my_uniform_int_distribution<IntType>::min() const -> result_type {
return lower;
}
template<class IntType>
auto my_uniform_int_distribution<IntType>::max() const -> result_type {
return upper;
}
int main() {
std::mt19937 foo;
my_uniform_int_distribution<int> bar(0,1000);
for (int i=0; i<99; ++i) {
bar(foo);
}
std::cout << bar(foo) << std::endl;
return 0;
}
此代码在我测试过的所有平台上打印出 490。
关于C++11 交叉编译器/标准库随机分布再现性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42084074/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我不知道为什么,但是当我设置这个设置时它无法编译设置:static_cache_control,[:public,:max_age=>300]这是我得到的syntaxerror,unexpectedtASSOC,expecting']'(SyntaxError)set:static_cache_control,[:public,:max_age=>300]^我只想将“过期”header设置为css、javaascript和图像文件。谢谢。 最佳答案 我猜您使用的是Ruby1.8.7。Sinatra文档中显示的语法似乎是在Ruby1.
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
运行bundleinstall后出现此错误:Gem::Package::FormatError:nometadatafoundin/Users/jeanosorio/.rvm/gems/ruby-1.9.3-p286/cache/libv8-3.11.8.13-x86_64-darwin-12.gemAnerroroccurredwhileinstallinglibv8(3.11.8.13),andBundlercannotcontinue.Makesurethat`geminstalllibv8-v'3.11.8.13'`succeedsbeforebundling.我试试gemin
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
我正在运行Ubuntu11.10并像这样安装Ruby1.9:$sudoapt-getinstallruby1.9rubygems一切都运行良好,但ri似乎有空文档。ri告诉我文档是空的,我必须安装它们。我执行此操作是因为我读到它会有所帮助:$rdoc--all--ri现在,当我尝试打开任何文档时:$riArrayNothingknownaboutArray我搜索的其他所有内容都是一样的。 最佳答案 这个呢?apt-getinstallri1.8编辑或者试试这个:(非rvm)geminstallrdocrdoc-datardoc-da
有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=
是否有适用于Ruby语言的.NETFramework编译器?我听说过DLR(动态语言运行时),这是否将使Ruby能够用于.NET开发? 最佳答案 IronRuby是Microsoft支持的项目,建立在动态语言运行时之上。 关于.net-是否有Ruby.NET编译器?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/199638/