I'm new at C++ and I'm trying to right a program that will generate random numbers from a range of 1-19. Here's how it should work, the program will generate 5 random numbers all ranged 1-19 and in random order. Once it chooses the first number, that number used for position 1(first number) cannot be used again, and after the second number is chosen it cannot be used again and so on. So basically it's like

postion one-19 possibilities
position two -18 possibilities (1-19 excluding position one number)

and so on until we get to position 5. What I need is for the program to print out all possible combinations of these numbers. I know thats gonna be a ton of results, but it's what I need. So if anyone can help me? I know I need to use an array to get unique numbers for each position and all, I just don't know how to do it. Here's the program I have so far, it just prints out one random number 1-19:

``````int main()
{
srand((unsigned)time(0));
int a;
int lowest=1, highest=19;
int range=(highest-lowest)+1;
for(int index=0; index<1; index++){
a = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout << a << endl;
}

}``````

## All 7 Replies

You can:

• create an array of 5 elements (either statically or on the fly )
• run a loop which will generate random number
• search whether that random number is already in the array
• as long as the number is one present in the array, keep on generating random numbers.
• If no match is found, insert that number into the array.
• Keep doing this till all the array slots are filled with random numbers.

Though the method is a bit ad hoc ( searching the entire array each time a random number is generated is bit too much) but should suffice your purpose.

Also why is your random generation method so complicated... Why not do something simple as: `random_number = min + rand( ) % max ;` This will generate random numbers between minimum and maximum, both inclusive.

Also for permutation purpose, you can try here... Though the algorithm is for permutation of character arrays, you can easily adapt it for integer ones.

Another confusing request...

I'm new at C++ and I'm trying to right a program that will generate random numbers from a range of 1-19. Here's how it should work, the program will generate 5 random numbers all ranged 1-19 and in random order. Once it chooses the first number, that number used for position 1(first number) cannot be used again, and after the second number is chosen it cannot be used again and so on. So basically it's like

postion one-19 possibilities
position two -18 possibilities (1-19 excluding position one number)

and so on until we get to position 5.

This claims you want random numbers, but this:

What I need is for the program to print out all possible combinations of these numbers.

claims you want all permutations. You can't do both. If you generate random numbers you can never be guaranteed to generate all permutations randomly. I see 2 programs here
1) generate 5 random numbers
2) generate a list of permutations.

~s.o.s~ has basically given you the help you need for each question. Although I have to say that for readability `random_number = min + rand( ) % max ;` needs parentheses: `random_number = min + (rand( ) % max);`

I am new to Daniweb, and some how cant find where and how to start a new thread. I have a problem with random number generation, and here it is:

I need to build a matrix of random number [1000,38] where each column vector is normally distributed, between -1 and +1, and the row vectors are uniformly distributed between -1 and +1

In simple language the 38 element vector is uniformly distributed and the 1000 element vector is normally distributed.

Any inputs.....?

Rajeev

> where each column vector is normally distributed, between -1 and +1, and the row vectors are uniformly distributed between -1 and +1
this is mathematically impossible. if row vectors are uniformly distributed between -1 and +1, any element in any row (read any element in your 2d array) is equally likely to have any value in the range (-1,+1) and therefore the column vectors will also be uniformly distributed. standard normal distribution is the normal distribution with a mean of zero and a variance of one; about 99.7% of the sample points would lie in the range (-3,+3), only about 68% would lie in the range (-1,+1). the normal distribution is a *continuous* distribution, theoretically in the range(-infinity,+infinity).

> where each column vector is normally distributed, between -1 and +1, and the row vectors are uniformly distributed between -1 and +1
this is mathematically impossible. if row vectors are uniformly distributed between -1 and +1, any element in any row (read any element in your 2d array) is equally likely to have any value in the range (-1,+1) and therefore the column vectors will also be uniformly distributed. standard normal distribution is the normal distribution with a mean of zero and a variance of one; about 99.7% of the sample points would lie in the range (-3,+3), only about 68% would lie in the range (-1,+1). the normal distribution is a *continuous* distribution, theoretically in the range(-infinity,+infinity).

I think you are right, Vijayan.....the problem i am facing is that the column vectors represent prices of commodities, which ar einherently normally distributed. There are 38 such commodities, and I would like to simulate correlated prices out of a set of 38 uncorrelated uniformly distributed random number ( using Cholesky's Decomposition).

If i start with 1000 uniformly distributed random numbers and then convert them into Normally ditributed numbers, and have 38 such sets, there is no guarantee that the 1000 corresponding sets of 38 numbers shall be uniformly distributed. It does not matter what range they are within.

Any further clues?

>> If i start with 1000 uniformly distributed random numbers and then convert them into Normally ditributed numbers, and have 38 such sets, there is no guarantee that the 1000 corresponding sets of 38 numbers shall be uniformly distributed. It does not matter what range they are within.

could this work? generate 38000 uniformly distributed random numbers. take the largest 1000 of these and convert them to normally ditributed numbers, repeat for the next largest 1000 and so on till you have 38 sets. don't know enough statistics to say for sure. but it does look intuitive.

hye..i'm new here..
can u help me to do this program...

SQUARE MATRIX OPERATIONS PROGRAM

PROBLEM STATEMENT

Write a menu driven program for performing matrix operations. Your program should generate two random
integer matrices A and B, and should be able to perform the following matrix operations:

1. Add two matrices A and B and save the result in C.
2. Subtract two matrices A and B and save the result in C.
3. Multiply two matrices A and B and save the result in C.
4. Dot‐Multiply two matrices A and B and save the result in C. (dot‐mult for element by element
multiplication)
5. Compute the transpose two matrices A and B and save the result in AT and BT respectively.
6. Compute the determinant of A and B.

PROBLEM ANALYSIS

Use separate functions for performing the various matrix operations. The main program should obtain user input
and transfer control to the functions that perform the desired operations. The program should ask the user to
input the size of the matrix, it must be flexible between 1x1 up to 10x10. Generate the random integer matrix, and
then do the operations according to the user choice.

The program consist of at least functions as follows:
1. main() – main program, to call various functions and controls the operations of the program.
2. matrix_menu() – display the program name, the programmer information (name, matric number and
section), ask to user to enter the size of matrix, display a list of choices of operation and obtains user
selection.
3. matrix_rand() – to generate random integer values of the matrix.
4. matrix_output() – function to print a matrix.
6. matrix_sub() – matrix subtraction function. Compute C=A ‐ B
7. matrix_mult() – matrix multiplication function. Compute C=A X B
8. matrix_dotmult() – matrix dot‐multiplication function. Compute C=A . B
9. matrix_transponse() – matrix transpose function. Compute AT=AT.
10. matrix_det() – matrix determinant function.