C++编程distribution问题 C++产生随机数

C++ \u7528\u7f16\u7a0b\u5199\u51fa\u6570\u5b66\u6982\u7387\u95ee\u9898

\u6570\u5b66\u7ed3\u679c\u5c31\u662f5/6\uff0c\u5e72\u561b\u8fd8\u8981\u9a8c\u8bc1\u3002\u3002
#include #include #include using namespace std;int main() {unsigned seed = chrono::system_clock::now().time_since_epoch().count();mt19937 g(seed);uniform_real_distribution d(0.0, 1.0);const unsigned N = 10000;unsigned count = 0;for (unsigned i = 0; i != N; ++i)if (d(g) + d(g) + d(g) > 1) ++count;cout << count << "/" << N << endl;return 0;}

C\u6807\u51c6\u5e93\u4e2d\u5b9a\u4e49\u7684rand\uff1a
\u53ea\u4fdd\u8bc1\u4e00\u6b21\u8c03\u7528\u6240\u8fd4\u56de\u7684\u503c\u662f\u201c\u8fd1\u4f3c\u4e8e\u968f\u673a\u7684\u201d\uff0c\u5e76\u4e0d\u4fdd\u8bc1\u591a\u6b21\u8c03\u7528\u8fd4\u56de\u503c\u7684\u5206\u5e03\uff08\u5177\u4f53\u7684\u5e73\u53f0\u3001\u7f16\u8bd1\u73af\u5883\u4e0b\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\uff09\u3002

\u5747\u5300\u5206\u5e03\uff0c\u6240\u6709\u70b9\u7684\u6982\u7387\u90fd\u4e00\u6837\uff0c\u5206\u5e03\u66f2\u7ebf\u662f\u5e73\u884c\u4e8ex\u8f74\u7684\uff0c\u548c\u5de6\u53f3\u8fb9\u754c\u6784\u6210\u4e00\u4e2a\u201c\u77e9\u5f62\u201d\u3002\u6211\u731c\u6d4b\uff0c\u591a\u6b21\u8c03\u7528rand\u7684\u5206\u5e03\uff0c\u7b26\u5408\u5747\u5300\u5206\u5e03\u7684\u53ef\u80fd\u6027\u66f4\u5927\u3002\u8fd9\u662f\u4e2a\u503c\u5f97\u63a2\u7a76\u7684\u95ee\u9898\uff0c\u9898\u4e3b\u53ef\u4ee5\u81ea\u884c\u5b9e\u9a8c\u9a8c\u8bc1\u4e4b\u3002

\u4e0b\u9762\u56de\u7b54\u4f60\u7684\u95ee\u9898\uff0c\u6700\u76f4\u63a5\u7684\u529e\u6cd5\u5c31\u662f\u2014\u2014\u7528\u6b63\u6001\u5206\u5e03\uff1a


C++0x \u6807\u51c6\u4e2d\u65b0\u52a0\u4e86\u4e13\u95e8\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u751f\u6210\u5747\u5300\u5206\u5e03\u3001\u6b63\u6001\u5206\u5e03\uff08Normal Distribution\uff09\u3001\u6cca\u677e\u5206\u5e03\uff08Binomial Distribution\uff09\u7684\u968f\u673a\u6570\u3002
\u7684\u8be6\u7ec6\u4ecb\u7ecd\u8bf7\u770b\uff1a http://www.cplusplus.com/reference/random/

\u6b63\u6001\u5206\u5e03\u7684 \u6982\u7387\u5bc6\u5ea6\u51fd\u6570\uff08\u503c\u4e0e\u5728\u8be5\u503c\u4e0a\u7684\u6982\u7387\u6784\u6210\u7684\u51fd\u6570\uff0cx -> p(x) \uff09\uff0c\u662f\uff1a

\u4e0d\u7528\u7ba1\u53f3\u8fb9\u8fd9\u4e48\u590d\u6742\u7684\u5173\u7cfb\u662f\u600e\u4e48\u6765\u7684\uff0c\u53ea\u9700\u8981\u77e5\u9053 x -> p(x) \u6709\u4e24\u4e2a\u53c2\u6570u, \u03c3\u5206\u522b\u8868\u793a\uff1a
u \u5747\u503c\uff08\u63a7\u5236\u7740\u56fe\u50cf\u4e0a\u7684\u5bf9\u79f0\u8f74\uff09
\u03c3 \u6807\u51c6\u5dee\uff08\u63a7\u5236\u7740\u56fe\u50cf\u4e0a\u7684\u201c\u5c71\u5cf0\u201d\u5f62\u7684\u201c\u5761\u5ea6\u201d\uff09

\u6765\u81eacplusplus.com\u7684\u4e00\u4e2a\u4ee3\u7801\u6837\u4f8b\uff08c++0x\u9700\u8981\u5728g++ 4.4 \u6216 vc2008\u4ee5\u4e0a\u7248\u672c\u7f16\u8bd1\uff09\uff1a
// normal_distribution#include #include int main(){ const int nrolls=10000; // number of experiments const int nstars=100; // maximum number of stars to distribute std::default_random_engine generator; std::normal_distribution distribution(5.0,2.0); // \u5747\u503c\u3001\u6807\u51c6\u5dee int p[10]={}; for (int i=0; i=0.0)&&(number<10.0)) ++p[int(number)]; } std::cout << "normal_distribution (5.0,2.0):" << std::endl; for (int i=0; i<10; ++i) { std::cout << i << "-" << (i+1) << ": "; std::cout << std::string(p[i]*nstars/nrolls,'*') << std::endl; } return 0;}\u8fd9\u6bb5\u4ee3\u7801\u7684\u8f93\u51fa\uff1a
normal_distribution (5.0,2.0):0-1: *1-2: ****2-3: *********3-4: ***************4-5: ******************5-6: *******************6-7: ***************7-8: ********8-9: ****9-10: *
\u5f53\u7136\uff0c\u9664\u4e86\u6b63\u6001\u5206\u5e03\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u529e\u6cd5\uff0c\u6bd4\u5982\u5728\u5747\u5300\u968f\u673a\u7684\u57fa\u7840\u4e0a\u505a\u201c\u6620\u5c04\u201d\uff0c\u8ba9\u66f4\u591a\u533a\u95f4\u90fd\u4e3a0.

\u76f8\u4fe1\u9898\u4e3b\u80fd\u591f\u901a\u8fc7\u8fd9\u4e9b\u4e1c\u897f\uff0c\u81ea\u5df1\u627e\u5230\u7b54\u6848\u3002

#include <iostream>
using std::cout;
using std::endl;

#define MAXLEN 36 // 单词最大长度

int strlength(char s[]) {
int len = 0;
while(s[len]) ++len;
return len;
}

int main() {
int wordlen[MAXLEN + 1] = {0};
FILE *inf;
char word[MAXLEN];
int i,len,total,others = 0;
char filename[] = "users/resources/ebook";
inf = fopen(filename,"rt");
while(!feof(inf)) {
fscanf(inf,"%s",word);
len = strlength(word);
if(len > 0 && len <= MAXLEN) ++wordlen[len];
else if(len) ++others;
}
fclose(inf);
total = 0;
for(i = 1; i <= MAXLEN; ++i)
total += wordlen[i];
total += others;
if(total) {
cout << "长度" << "数量" << "百分比
";
for(i = 1; i <= MAXLEN; ++i)
if(wordlen[i])
cout << i << "" << wordlen[i] << "" << (double)wordlen[i]/total << endl;
if(others) cout << "其他" << others << "" << (double)others/total << endl;
}
cout << filename << " is empty
";
return 0;
}


#include <map>
#include <string>
#include <fstream>
#include <iostream>

int main(int argc, char *argv[])
{
using namespace std;

ifstream fin("users/resources/ebook");
string word;
// map<string, int> dict; // 如果统计每个单词的频率,可以用 map<string, int>
map<int, int> freq; // 统计每种长度出现的次数

while( fin >> word ) 
{
// dict[word]++;
freq[word.length()]++;
}

cout << "LengthCount
";
for(map<int, int>::iterator it = freq.begin();
it != freq.end(); ++it) 
{
cout << it->first << "" << it->second << "
";
}
return 0;
}


使用输入文件流,经过string类型循环来判断。

  • 鐢C璇煶缂栫▼瀹炵幇:灏忔槑鏈5鏈柊涔,瑕佸熺粰A,B,C ,涓変綅鍚屽,姣忎汉鍙兘鍊熶竴...
    绛旓細绋嬪簭璇存槑涓庢敞閲 void main(){ int a,b,c,count=0;printf("There are diffrent methods for XM to distribute books to 3 readers:\n");for(a=1;a<=5;a++) /*绌蜂妇绗竴涓汉鍊5鏈功涓殑1鏈殑鍏ㄩ儴鎯呭喌*/ for(b=1;b<=5;b++) /*绌蜂妇绗簩涓汉鍊5鏈功涓殑涓鏈殑鍏ㄩ儴鎯呭喌*/ for(c...
  • 璺眰澶х甯,缁欎釜涓嶅皯浜60琛岀殑C璇█C++缂栫▼,瑕佹眰甯︿笂妗堜緥,鍜岃繍琛岀粨鏋...
    绛旓細include <string.h> include <stdio.h> include <iostream.h> define FALSE 0 define TRUE 1 define W 5 define R 3 int ORDER[W];int M ; // 鎬昏繘绋嬫暟 int N ; // 璧勬簮绉嶇被 int ALL_RESOURCE[R]={10,5,7}; // 鍚勭璧勬簮鐨勬暟鐩诲拰 int MAX[W][R]={7,5,3,3...
  • linux c鍐呭瓨婧㈠嚭鐨刢ore dump bug鎬庝箞璺
    绛旓細褰撲竴涓▼搴忓穿婧冩椂锛屽湪杩涚▼褰撳墠宸ヤ綔鐩綍鐨刢ore鏂囦欢涓鍒朵簡璇ヨ繘绋嬬殑瀛樺偍鍥惧儚銆俢ore鏂囦欢浠呬粎鏄竴涓唴瀛樻槧璞(鍚屾椂鍔犱笂璋冭瘯淇℃伅)锛屼富瑕佹槸鐢ㄦ潵璋冭瘯鐨勩傚綋绋嬪簭鎺ユ敹鍒颁互涓婾NIX淇″彿浼氫骇鐢焎ore鏂囦欢锛氬悕瀛 璇存槑 ANSI C POSIX.1 SVR4 4.3+BSD 缂虹渷鍔ㄤ綔 SIGABRT 寮傚父缁堟(abort). .. .缁堟w/core SIGBUS ...
  • 鑳界粰鎴c/c++涓插彛閫氫俊鍏稿瀷搴旂敤瀹炰緥缂栫▼瀹炶返鐨勭數瀛愮増鍚,杩樻湁婧愪唬鐮乢鐧惧害...
    绛旓細then you are only allowed to distribute versions released by the author. This is to maintain a single distribution point for the source code. */// Macros / Structs etc ///#pragma once#ifndef __SERIALPORT_H__#define __SERIALPORT_H__#ifndef CSERIALPORT_EXT_CLASS#define CSERIALPO...
  • C璇█鏈夊叧鐨勯
    绛旓細if(c!=a&&c!=b) /*鍒ゆ柇绗笁浜轰笌鍓嶄袱涓汉鍊熺殑涔︽槸鍚︿笉鍚*/ printf(count%8?"%2d:%d,%d,%d ":"%2d:%d,%d,%d\\n ",++count,a,b,c);/*鎵撳嵃鍙兘鐨勫熼槄鏂规硶*/ } 杩愯缁撴灉 There are diffrent methods for XM to distribute books to 3 readers:1: 1,2,3 2: 1,2,4...
  • 姹C缂栫▼瀹炰緥
    绛旓細if(c!=a&&c!=b) /*鍒ゆ柇绗笁浜轰笌鍓嶄袱涓汉鍊熺殑涔︽槸鍚︿笉鍚*/ printf(count%8?"%2d:%d,%d,%d ":"%2d:%d,%d,%d\\n ",++count,a,b,c); /*鎵撳嵃鍙兘鐨勫熼槄鏂规硶*/}*杩愯缁撴灉 There are diffrent methods for XM to distribute books to 3 readers: ...
  • 扩展阅读:学习c++用什么软件 ... 手机c++编程软件 ... 信息学奥赛c++编程 ... 如何自己编程做游戏 ... c++和python先学哪个 ... 一节课教你学会c++视频 ... c++编程适合几岁学 ... c++免费视频教程 ... 一节课就能学会c++的视频 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网