Hi friends,
I am a final year Mechanical Student and I am doing my Final year project in C++. My project requires some complicated programming in C++..
Here are the details.
Initially I need to create a random matrix.
The user will give the no of rows and columns.
For example, if the user gives no of rows as 5 and no of columns as 5, the program must randomly generate 5*5 matrix.
Also the numbers must range from 0 to 1.5 only.. All are decimal numbers..
For example, if it is a 5*5 matrix, all 25 numbers should not be non zero. Almost 15 or 16 numbers should be zero. The rest of the non zero numbers should be dispersed such that no row contains all numbers as zeros..
This must work out for any number of row or column..
Next step is even more difficult.
The program must shuffle the rows and columns of this matrix and obtain 10 new matrices..
The initial matrix should be rearranged randomly to give these 10 matrices.
After this I have to apply PSO algorithm to these matrices. But that is only after this program.
Please Help friends.
Thank You.

I think the stl container template, std::vector<T> will help you with the dynamic allocation, and the STL algorithm std::random_shuffle() will help you obtain new matrices--as for the random number generation, if you have C++0x TR1 support on your compiler, you can use the new random number generation facilities which includes distributions. Otherwise, the C function rand() will generate random numbers, and you will have to make do with them, probably by dividing by 10 until you get a decimal (or something more creative).

Of course you may opt to not use the vector or standard library algorithms, it is just a possibility and will make it much easier to do the assignment.

Edited 5 Years Ago by pseudorandom21: n/a

You have not specified the problem fully enough to make it possible to think about how to solve it.

For example, first you say that the program should randomly generate 25 numbers, and then you say that "almost 15 or 16 numbers" (whatever that means) should be zero.

It is hard to understand how to meet these requirements at the same time. For example, it is acceptable to begin by deciding which elements will be zero and which ones nonzero? Are there any requirements on the distribution of the nonzero elements aside from the requirement that no row or column be entirely zero? Is the following algorithm acceptable?

For each row, pick one column at random that has not already been picked and remember what it is. If you make those elements nonzero, it will satisfy the aforementioned requirement, but the choice of elements will hardly be random, because exactly one element will be nonzero in each row or column.

Until you are certain that you understand the problem, there is little to gain in looking for solutions.

There are no other constraints. As you have asked, let me explain my problem in detail. In my program, rows represent machines and columns represent products manufactured using these machines. If there are 3 machines and 4 products, we have to generate a 3*4 random matrix.
M/P p1 P2 P3 P4
M1 0 0.5 0 0
M2 1 0 0.25 1.1
M3 0 0 1 0
The above matrix is a sample problem. Note that there are 7 zeros and 5 non zero numbers present. This 7 and 5 allocation must also be random but mostly zeros should be present. Also no rows or columns must be fully zero.
If a row is fully zero, it means that the machine is not at all used. If a column is fully zero, it means that the product is not at all manufactured. So both should not happen.
Hope this information will be useful for us to proceed further.
Thanks again.

Edited 5 Years Ago by Prasanna Venkat: n/a

Have you written any code yet? If so, what do you have? It would be a good idea for you to share the relevant sections of it.

From an educational standpoint, it is better for you if you let us see it and discuss it with you and/or critique it. We're not going to simply write it for you, all that does is get you an easy grade and doesn't teach you anything.

Edited 5 Years Ago by Fbody: n/a

I'm sorry, but saying "this 7 and 5 allocation must also be random" is not a specification. In general, you have to say much more than just using the word "random" in order to make it clear what problem you're trying to solve.

For example, suppose I tell you that I want a variable to have a random floating-point value between 0 and 1. Is it acceptable to toss a coin and set that variable to 0.1 for heads and 0.9 for tails? Why or why not?

What if I pick two random integers and use them as the value for the fraction and exponent, subject to the restriction that the exponent will never be so large that it causes the number to be greater than 1. Is that approach acceptable? Even though it will cause the distribution of the values to be heavily skewed toward very small numbers?

I ask these questions because in your most recent message, you say two things that cannot both be true at once: (1) You want a 3x4 random matrix; (2) The elements of the matrix must be mostly zero.

If most of the elements are zero, then it's not random!!! So it won't do to say that it is random; you have to say just what characteristics you want it to have. And until you can explain what those characteristics are, there is nothing that anyone else can do to help you.

For example, suppose I tell you that I want a variable to have a random floating-point value between 0 and 1. Is it acceptable to toss a coin and set that variable to 0.1 for heads and 0.9 for tails? Why or why not?

What if I pick two random integers and use them as the value for the fraction and exponent, subject to the restriction that the exponent will never be so large that it causes the number to be greater than 1. Is that approach acceptable? Even though it will cause the distribution of the values to be heavily skewed toward very small numbers?

I ask these questions because in your most recent message, you say two things that cannot both be true at once: (1) You want a 3x4 random matrix; (2) The elements of the matrix must be mostly zero.

If most of the elements are zero, then it's not random!!! So it won't do to say that it is random; you have to say just what characteristics you want it to have. And until you can explain what those characteristics are, there is nothing that anyone else can do to help you.

Most of the numbers should be zero means some must be non zero too.. We know that half the numbers will be zero. But we don't know where these zeros will come and where these non zeros will come. That should be random i said. The positioning of zero and non zero numbers should be random. Also the values of non zero numbers must be random.
The matrix represents machining time of each product in each machine.
As we all know, in an industry the machining time for a product will be less than a minute. Thats why i gave the lower and upper limits as 0 and 1.5 minutes.

Picking only two numbers as values will not be a good choice as all products wont have same machining time in different machines.

So it must be a random generation only.

I think what you should do is get the dimensions of the array, then generate the array as all zeros. Then, once the array is initialized, use some RNG-based algorithm to determine how many numbers to generate and where to place them.

This way, you still get "random" placement and you get random values, but the number of zeros is still under your control.

Edited 5 Years Ago by Fbody: n/a

I think the easiest way to generate your numbers might be something like this:

double d = (rand() % 3000 - 1500) * 0.001;
d = ( d > 0.0 ? d : 0.0);

This way, roughly half of the numbers will be zero and the other half will be between 0.001 and 1.500. I think that should do it. For the rows and columns being all non-zero, you could do this kinda pseudo-code:

while matrix_has_a_zero_row_or_col(m)
  for each row i
    for each column j
      m[i,j] = generate_rand_number_as_above();

Just write a function that verifies the condition on the rows and columns and repeat the random generation of the matrix until the condition is satisfied. That IMO is the least biased method to do it.

I think the tr1 random facilities may help. If you're using Visual Studio 2010, this may be worth looking into:

#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int main()
{
	std::tr1::uniform_real<double> dist( 0.0, 1.0);
	std::tr1::mt19937 engine(time(nullptr));
	for(int i = 0; i < 20; i++) //print a few random numbers.
	std::cout << dist(engine) << std::endl;
	std::cin.get();
}
This article has been dead for over six months. Start a new discussion instead.