pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<tfoot id='YGXnX'></tfoot>
  • <small id='YGXnX'></small><noframes id='YGXnX'>

      <bdo id='YGXnX'></bdo><ul id='YGXnX'></ul>

    <i id='YGXnX'><tr id='YGXnX'><dt id='YGXnX'><q id='YGXnX'><span id='YGXnX'><b id='YGXnX'><form id='YGXnX'><ins id='YGXnX'></ins><ul id='YGXnX'></ul><sub id='YGXnX'></sub></form><legend id='YGXnX'></legend><bdo id='YGXnX'><pre id='YGXnX'><center id='YGXnX'></center></pre></bdo></b><th id='YGXnX'></th></span></q></dt></tr></i><div class="xtprxld" id='YGXnX'><tfoot id='YGXnX'></tfoot><dl id='YGXnX'><fieldset id='YGXnX'></fieldset></dl></div>

        <legend id='YGXnX'><style id='YGXnX'><dir id='YGXnX'><q id='YGXnX'></q></dir></style></legend>

        為什么不直接使用random_device?

        Why not just use random_device?(為什么不直接使用random_device?)

            <tbody id='mAXb6'></tbody>

                • <bdo id='mAXb6'></bdo><ul id='mAXb6'></ul>
                • <tfoot id='mAXb6'></tfoot>
                • <legend id='mAXb6'><style id='mAXb6'><dir id='mAXb6'><q id='mAXb6'></q></dir></style></legend>

                  <small id='mAXb6'></small><noframes id='mAXb6'>

                  <i id='mAXb6'><tr id='mAXb6'><dt id='mAXb6'><q id='mAXb6'><span id='mAXb6'><b id='mAXb6'><form id='mAXb6'><ins id='mAXb6'></ins><ul id='mAXb6'></ul><sub id='mAXb6'></sub></form><legend id='mAXb6'></legend><bdo id='mAXb6'><pre id='mAXb6'><center id='mAXb6'></center></pre></bdo></b><th id='mAXb6'></th></span></q></dt></tr></i><div class="lrhvzd7" id='mAXb6'><tfoot id='mAXb6'></tfoot><dl id='mAXb6'><fieldset id='mAXb6'></fieldset></dl></div>
                  本文介紹了為什么不直接使用random_device?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我對 c++11 隨機庫有點困惑.

                  我的理解:我們需要兩個獨立的概念:

                  • 隨機引擎(可以是偽(需要種子)或真實的)
                  • 分布:它使用特定分布將從引擎獲得的數(shù)字映射到特定區(qū)間.

                  我不明白為什么不直接使用這個:

                  std::random_device rd;std::uniform_int_distribution分布(1, 5);//獲取隨機數(shù):區(qū)(rd);

                  據(jù)我所知這很有效.

                  相反,這是我在大多數(shù)示例/網(wǎng)站/文章中發(fā)現(xiàn)的:

                  std::random_device rd;std::mt19937 e{rd()};//或 std::default_random_engine e{rd()};std::uniform_int_distribution距離{1, 5};//獲取隨機數(shù):分布(e);

                  我不是在談?wù)撎厥庥猛荆缑艽a學(xué),只是您的基本入門文章.

                  我的懷疑是因為 std::mt19937(或 std::default_random_engine)接受種子,通過在調(diào)試期間提供相同的種子可以更容易地調(diào)試會話.

                  另外,為什么不只是:

                  std::mt19937 e{std::random_device{}()};

                  解決方案

                  另外,為什么不只是:

                  std::mt19937 e{std::random_device{}()};

                  如果你只做一次可能沒問題,但如果你會做很多次,最好跟蹤你的 std::random_device 并且不要不必要地創(chuàng)建/銷毀它.

                  查看 std::random_device 實現(xiàn)的 libc++ 源代碼可能會有所幫助,它非常簡單.它只是對 std::fopen("/dev/urandom") 的一個薄包裝.因此,每次創(chuàng)建 std::random_device 時,您都會獲得另一個文件系統(tǒng)句柄,并支付所有相關(guān)費用.

                  據(jù)我所知,在 Windows 上,std::random_device 代表對微軟加密 API 的一些調(diào)用,因此每次執(zhí)行此操作時,您都將初始化和銷毀??一些加密庫接口.>

                  這取決于您的應(yīng)用程序,但出于一般目的,我不會認為這種開銷總是可以忽略不計.有時是這樣,然后這很棒.

                  我想這與您的第一個問題有關(guān):

                  <塊引用>

                  相反,這是我在大多數(shù)示例/網(wǎng)站/文章中發(fā)現(xiàn)的:

                   std::random_device rd;std::mt19937 e{rd()};//或 std::default_random_engine e{rd()};std::uniform_int_distribution距離{1, 5};

                  至少我是這么認為的:

                  • std::mt19937 是一個非常簡單可靠的隨機生成器.它是獨立的,將完全存在于您的進程中,不會調(diào)用操作系統(tǒng)或其他任何東西.該實現(xiàn)由標準強制,至少在 boost 中,它在任何地方都使用相同的代碼,源自原始的 mt19937 論文.這段代碼非常穩(wěn)定,而且是跨平臺的.您可以非常確信,初始化、查詢等將在您編譯它的任何平臺上編譯為類似的代碼,并且您將獲得類似的性能.

                  • std::random_device 相比之下,它是相當不透明的.你并不真正知道它是什么,它會做什么,或者它的效率如何.你甚至不知道它是否真的可以被獲取——當你嘗試創(chuàng)建它時它可能會拋出異常.你知道它不需要種子.您通常不應(yīng)該從中提取大量數(shù)據(jù),只需使用它來生成種子即可.有時,它充當加密 API 的一個很好的接口,但實際上并不需要這樣做,遺憾的是有時也不需要.它可能對應(yīng)于 unix 上的 /dev/random,也可能對應(yīng)于 /dev/urandom/.它可能對應(yīng)于某些 MSVC 加密 API(visual studio),或者它可能只是一個固定常量(mingw).如果您為某些手機進行交叉編譯,誰知道它會做什么.(即使您確實獲得了 /dev/random,您仍然會遇到性能可能不一致 的問題——它可能看起來工作得很好,直到熵池用完,然后像狗一樣慢.)

                  我的想法是,std::random_device 應(yīng)該是 time(NULL) 播種的改進版本——這是一個低標準,因為 time(NULL) 是一個非常糟糕的種子.我通常在我會使用 time(NULL) 生成種子的地方使用它,回到當天.除此之外,我真的不認為它有什么用處.

                  I am a bit confused about the c++11 random library.

                  What I understand: we need two separate concepts:

                  • random engine (which can be pseudo (need seed) or real)
                  • distribution: it maps the numbers obtained from the engine to a specific interval, using a specific distribution.

                  What I don't understand is why not just use this:

                  std::random_device rd;
                  std::uniform_int_distribution<int> dist(1, 5);
                  
                  // get random numbers with:
                  dist(rd);
                  

                  As far as I can tell this works well.

                  Instead, this is what I found on most examples/sites/articles:

                  std::random_device rd;
                  std::mt19937 e{rd()}; // or std::default_random_engine e{rd()};
                  std::uniform_int_distribution<int> dist{1, 5};
                  
                  // get random numbers with:
                  dist(e);
                  

                  I am not talking about special use, e.g. cryptography, just your basic getting started articles.

                  My suspicion is because std::mt19937 (or std::default_random_engine) accepts a seed, it can be easier to debug by providing the same seed during a debug session.

                  Also, why not just:

                  std::mt19937 e{std::random_device{}()};
                  

                  解決方案

                  Also, why not just:

                  std::mt19937 e{std::random_device{}()};

                  It might be fine if you only will do this once, but if you will do it many times, it's better to keep track of your std::random_device and not create / destroy it unnecessarily.

                  It may be helpful to look at the libc++ source code for implementation of std::random_device, which is quite simple. It's just a thin wrapper over std::fopen("/dev/urandom"). So each time you create a std::random_device you are getting another filesystem handle, and pay all associated costs.

                  On windows, as I understand, std::random_device represents some call to a microsoft crypto API, so you are going to be initializing and destroying some crypto library interface everytime you do this.

                  It depends on your application, but for general purposes I wouldn't think of this overhead as always negligible. Sometimes it is, and then this is great.

                  I guess this ties back into your first question:

                  Instead, this is what I found on most examples/sites/articles:

                   std::random_device rd;
                   std::mt19937 e{rd()}; // or std::default_random_engine e{rd()};
                   std::uniform_int_distribution<int> dist{1, 5};
                  

                  At least the way I think about it is:

                  • std::mt19937 is a very simple and reliable random generator. It is self-contained and will live entirely in your process, not calling out to the OS or anything else. The implementation is mandated by the standard, and at least in boost, it used the same code everywhere, derived from the original mt19937 paper. This code is very stable and it's cross-platform. You can be pretty confident that initializing it, querying from it, etc. is going to compile to similar code on any platform that you compile it on, and that you will get similar performance.

                  • std::random_device by contrast is pretty opaque. You don't really know exactly what it is, what it's going to do, or how efficient it will be. You don't even know if it can actually be acquired -- it might throw an exception when you attempt to create it. You know that it doesn't require a seed. You're not usually supposed to pull tons and tons of data from it, just use it to generate seeds. Sometimes, it acts as a nice interface to cryptographic APIs, but it's not actually required to do that and sadly sometimes it doesn't. It might correspond to /dev/random on unix, it might correspond to /dev/urandom/. It might correspond to some MSVC crypto API (visual studio), or it might just be a fixed constant (mingw). If you cross-compile for some phone, who knows what it will do. (And even when you do get /dev/random, you still have the problem that performance may not be consistent -- it may appear to work great, until the entropy pool runs out, and then it runs slow as a dog.)

                  The way I think about it is, std::random_device is supposed to be like an improved version of seeding with time(NULL) -- that's a low bar, because time(NULL) is a pretty crappy seed all things considered. I usually use it where I would have used time(NULL) to generate a seed, back in the day. I don't really consider it all that useful outside of that.

                  這篇關(guān)于為什么不直接使用random_device?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  read input files, fastest way possible?(讀取輸入文件,最快的方法?)
                  The easiest way to read formatted input in C++?(在 C++ 中讀取格式化輸入的最簡單方法?)
                  Reading from .txt file into two dimensional array in c++(從 .txt 文件讀取到 C++ 中的二維數(shù)組)
                  How to simulate a key press in C++(如何在 C++ 中模擬按鍵按下)
                  Why doesn#39;t getline(cin, var) after cin.ignore() read the first character of the string?(為什么在 cin.ignore() 之后沒有 getline(cin, var) 讀取字符串的第一個字符?)
                  What is the cin analougus of scanf formatted input?(scanf 格式輸入的 cin 類比是什么?)
                    <bdo id='r8WIi'></bdo><ul id='r8WIi'></ul>

                        <legend id='r8WIi'><style id='r8WIi'><dir id='r8WIi'><q id='r8WIi'></q></dir></style></legend>

                          <i id='r8WIi'><tr id='r8WIi'><dt id='r8WIi'><q id='r8WIi'><span id='r8WIi'><b id='r8WIi'><form id='r8WIi'><ins id='r8WIi'></ins><ul id='r8WIi'></ul><sub id='r8WIi'></sub></form><legend id='r8WIi'></legend><bdo id='r8WIi'><pre id='r8WIi'><center id='r8WIi'></center></pre></bdo></b><th id='r8WIi'></th></span></q></dt></tr></i><div class="lnrpjpv" id='r8WIi'><tfoot id='r8WIi'></tfoot><dl id='r8WIi'><fieldset id='r8WIi'></fieldset></dl></div>

                          • <small id='r8WIi'></small><noframes id='r8WIi'>

                            <tfoot id='r8WIi'></tfoot>

                              <tbody id='r8WIi'></tbody>
                          • 主站蜘蛛池模板: 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | DWS物流设备_扫码称重量方一体机_快递包裹分拣机_广东高臻智能装备有限公司 | 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 | 数显恒温培养摇床-卧式/台式恒温培养摇床|朗越仪器 | 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 行业分析:提及郑州火车站附近真有 特殊按摩 ?2025实地踩坑指南 新手如何避坑不踩雷 | 不锈钢酒柜|恒温酒柜|酒柜定制|酒窖定制-上海啸瑞实业有限公司 | 中控室大屏幕-上海亿基自动化控制系统工程有限公司 | 冲锋衣滑雪服厂家-冲锋衣定制工厂-滑雪服加工厂-广东睿牛户外(S-GERT) | 鹤壁创新仪器公司-全自动量热仪,定硫仪,煤炭测硫仪,灰熔点测定仪,快速自动测氢仪,工业分析仪,煤质化验仪器 | 布袋除尘器-单机除尘器-脉冲除尘器-泊头市兴天环保设备有限公司 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 耐火砖厂家,异形耐火砖-山东瑞耐耐火材料厂 | 纸箱抗压机,拉力机,脂肪测定仪,定氮仪-山东德瑞克仪器有限公司 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 精密钢管,冷拔精密无缝钢管,精密钢管厂,精密钢管制造厂家,精密钢管生产厂家,山东精密钢管厂家 | 广州网站建设_小程序开发_番禺网站建设_佛山网站建设_粤联网络 | 河南橡胶接头厂家,河南波纹补偿器厂家,河南可曲挠橡胶软连接,河南套筒补偿器厂家-河南正大阀门 | 电镀标牌_电铸标牌_金属标贴_不锈钢标牌厂家_深圳市宝利丰精密科技有限公司 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 油冷式_微型_TDY电动滚筒_外装_外置式电动滚筒厂家-淄博秉泓机械有限公司 | 海南在线 海南一家| 深圳办公室装修-写字楼装修设计-深圳标榜装饰公司 | 单机除尘器 骨架-脉冲除尘器设备生产厂家-润天环保设备 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 酒瓶_酒杯_玻璃瓶生产厂家_徐州明政玻璃制品有限公司 | 连栋温室大棚建造厂家-智能玻璃温室-薄膜温室_青州市亿诚农业科技 | 卫生人才网-中国专业的医疗卫生医学人才网招聘网站! | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 东莞喷砂机-喷砂机-喷砂机配件-喷砂器材-喷砂加工-东莞市协帆喷砂机械设备有限公司 | 臭氧实验装置_实验室臭氧发生器-北京同林臭氧装置网 | 硬质合金模具_硬质合金非标定制_硬面加工「生产厂家」-西迪技术股份有限公司 | 深圳激光打标机_激光打标机_激光焊接机_激光切割机_同体激光打标机-深圳市创想激光科技有限公司 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 气密性检测仪_气密性检测设备_防水测试仪_密封测试仪-岳信仪器 | 亿立分板机_曲线_锯片式_走刀_在线式全自动_铣刀_在线V槽分板机-杭州亿协智能装备有限公司 | 天津蒸汽/热水锅炉-电锅炉安装维修直销厂家-天津鑫淼暖通设备有限公司 | 新材料分散-高速均质搅拌机-超声波分散混合-上海化烁智能设备有限公司 | 米顿罗计量泵(科普)——韬铭机械 | 土壤养分检测仪|土壤水分|土壤紧实度测定仪|土壤墒情监测系统-土壤仪器网 |