I have to create a random matrix m*n where m and n are user inputs.
Now i have to fill the matrix with random nos. Finally I must have a matrix where each value is either 0 , 1 , a fraction no between 0 and 1, and also sum of each column should be equal to 1. how should i create the matrix ??

thanks for help... :)

Dani AI

Generated

— the requirement in your post (each column sums to 1 and entries are 0, 1, or a fraction in (0,1)) describes a column‑stochastic matrix (each column is a probability distribution over the rows). The thread title’s phrase “without fractions” is contradictory: if fractions are forbidden the only integer solution is a one‑hot column (exactly one entry = 1, all others 0). Otherwise the usual, flexible approach is to generate non‑negative random values per column and normalize each column so its entries sum to 1. As asked, the snippet below is in C++11; as hinted, here’s a concrete effort you can adapt.

Algorithm sketch: for each column either (a) make it one‑hot (pick one row and set it to 1) or (b) pick k distinct row positions (k between 1 and m), draw k positive random reals, divide each by their sum (normalization) so the column sums to 1. Use one mt19937 seeded once; use double for precision; after normalization add the tiny correction (1.0 - actual_sum) to one nonzero entry to cancel floating‑point rounding.

// C++11: generate m x n matrix where each column sums to 1
#include <random>
#include <vector>
#include <numeric>
#include <algorithm>
#include <limits>

std::vector<std::vector<double>> gen_matrix(int m, int n, double one_hot_prob = 0.2) {
    if (m <= 0 || n <= 0) return {};
    std::random_device rd; std::mt19937 rng(rd());
    std::uniform_real_distribution<double> uni(0.0, 1.0);
    std::uniform_int_distribution<int> kdist(1, m);
    std::vector<std::vector<double>> A(m, std::vector<double>(n, 0.0));
    std::vector<int> idx(m);
    for (int j = 0; j < n; ++j) {
        if (uni(rng) < one_hot_prob) { std::uniform_int_distribution<int> id(0, m-1); A[id(rng)][j] = 1.0; continue; }
        int k = kdist(rng);
        std::iota(idx.begin(), idx.end(), 0); std::shuffle(idx.begin(), idx.end(), rng);
        double sum = 0.0;
        for (int t = 0; t < k; ++t) { double v = uni(rng); if (v == 0.0) v = std::numeric_limits<double>::min(); A[idx[t]][j] = v; sum += v; }
        if (sum == 0.0) { A[idx[0]][j] = 1.0; continue; }
        for (int t = 0; t < k; ++t) A[idx[t]][j] /= sum;
        double s2 = 0.0; for (int i = 0; i < m; ++i) s2 += A[i][j]; A[idx[0]][j] += 1.0 - s2;
    }
    return A;
}

Notes and pitfalls: to get strictly integer (no fractions) set one_hot_prob = 1.0. For large matrices avoid reseeding the RNG inside loops. Verify each column sum with an epsilon (e.g. fabs(sum-1.0) < 1e-12). In plain C the same idea can be implemented with rand() and scaling, but C++ <random> is recommended for better quality and range.

Recommended Answers

All 2 Replies

Since I would write the code differently in C or C++ maybe you need to specify the target language first?

Plus, this question is on the web so there's that.

I have to create a random matrix

You have to, so why should WE have to?
Show us your efforts, maybe we'll help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.