2017-04-24 76 views
-1
#**include "stdafx.h" 
#include <iostream> 
#include <process.h> 
#include <ctime> 
#include <cstdlib> 
#include <random> 
using namespace std; 
double printMatrix(); 
int main() 
{ 
    int n = 0, m = 1; 
    srand(time(0)); 
    rand() % 2; 
    double printMatrix(); 
    cout << "Enter the number of rows: "; 
    cin >> n; 
    cout << "Enter the number of columns: "; 
    cin >> m; 
    system("pause"); 
    return 0; 
} 
double printMatrix() 
{ 
    return (double)rand()/((double)RAND_MAX + 1); 
} 

我位於C真正的新++,我終於得到了提示用戶輸入多少行和列,但它也需要證明這一點:C++與隨機數只有0到1

{Num of rows:3                   Num of col: 2 
01 
00                      10} 
+0

你不熟悉使用循環? –

+2

推動X-Y方向:[考慮使用'std :: uniform_real_distribution'](http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution)。它需要一些設置(包括在鏈接中),但它確實是你想要的東西(改變示例代碼「std :: uniform_real_distribution <> dis(1,2);'到'std :: uniform_real_distribution <> dis(0,1);') – user4581301

+1

@CaptainObvlious C++沒有本地函數。聲明是指全球功能,無論它在哪裏。 – melpomene

回答

0

標準庫有一個名爲「隨機」的頭文件。

#include <random> 
#include <ctime> 

int main() 
{ 
    std::srand(std::time(nullptr)); 
    int x = std::rand() % 2; 
    return 0; 
} 
+0

好一提的是srand()函數應該只被調用一次,只有蘭特()變爲環... – didiz

+1

'的std :: srand'和'的std :: rand'從''而非破碎的RNG在'' user463035818

+0

我這麼失去了發現,我不明白環路 – LindaS

4

如果您正在使用C++ 11,你可以用:

#include <iostream> 
#include <random> 

static std::random_device rd; // Random device engine, usually based on /dev/random on UNIX-like systems 
static std::mt19937_64 rng(rd()); // Initialize Mersennes' twister using rd to generate the seed 

// Generate random doubles in the interval [initial, last) 
double random_real(double initial, double last) { 
    std::uniform_real_distribution<double> distribution(initial, last); 
    return distribution(rng); // Use rng as a generator 
} 

// Generate random int in the interval [initial, last] 
int random_int(int initial, int last) { 
    std::uniform_int_distribution<int> distribution(initial, last); 
    return distribution(rng); // Use rng as a generator 
} 

int main() { 
    int rows, columns; 
    std::cout << "Enter the number of rows: "; 
    std::cin >> rows; 
    std::cout << "Enter the number of columns: "; 
    std::cin >> columns; 
    // For print a binary matrix. 
    std::cout << "Binary Matrix" << std::endl; 
    for (int i = 0; i < rows; ++i) { 
     for (int j = 0; j < columns; ++j) { 
      std::cout << random_int(0, 1) << " "; 
     } 
     std::cout << std::endl; 
    } 

    // For print a matrix with doubles in interval [0, 1) 
    std::cout << "Double Matrix" << std::endl; 
    for (int i = 0; i < rows; ++i) { 
     for (int j = 0; j < columns; ++j) { 
      std::cout << random_real(0, 1) << " "; 
     } 
     std::cout << std::endl; 
    } 
} 

例如執行:

❯❯❯ g++ ../test.cc -std=c++11 -o test && ./test 
Enter the number of rows: 3 
Enter the number of columns: 4 
Binary Matrix 
0 1 0 0 
1 0 1 0 
0 0 1 0 
Double Matrix 
0.33597 0.384928 0.062972 0.109735 
0.689703 0.111154 0.0220375 0.260458 
0.826409 0.601987 0.94459 0.442919 
+0

我認爲'fprintf'不在''。 – melpomene

+1

注意'RD()'沒有足夠的熵種子'的std :: mt19937_64',所以你就必須只可能序列的一小部分的訪問。即:不要將這種播種方法用於密碼或科學目的。 – Richard

+1

警告:'std :: random_device'實現不佳,並且在GCC的常見Windows版本中始終返回相同的數字。 OP不是問題,因爲它看起來在使用Visual Studio,但未來的讀者會被警告。 – user4581301