| | |
C++ Random Number Generator
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 2
Reputation:
Solved Threads: 0
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:
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:
C++ Syntax (Toggle Plain Text)
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; } }
Last edited by ~s.o.s~; Jan 29th, 2007 at 12:53 pm. Reason: Added code tags, learn to use them.
You can:
Also why is your random generation method so complicated... Why not do something simple as:
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.
- 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.
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.
I don't accept change; I don't deserve to live.
Another confusing request...
This claims you want random numbers, but this:
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
needs parentheses:
•
•
•
•
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.
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); The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Aug 2007
Posts: 4
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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).
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).
•
•
Join Date: Aug 2007
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
> 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).
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?
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
>> 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.
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.
Last edited by vijayan121; Aug 28th, 2007 at 10:51 am.
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
-1
#8 Oct 13th, 2009
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.
5. matrix_add() – matrix addition function. Compute C=A + B
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.
Additional info:
‐ For random integer you can use srand() and this function
int rand_int(int a, int b)
{ return rand()%(b-a+1) + a; }
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.
5. matrix_add() – matrix addition function. Compute C=A + B
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.
Additional info:
‐ For random integer you can use srand() and this function
int rand_int(int a, int b)
{ return rand()%(b-a+1) + a; }
![]() |
Similar Threads
- Random number generator's (C++)
- Realy weird random number generator bug (C++)
- Random number generator (Java)
- Random number generator (Java)
- Random number generator (Java)
- random number generator (C++)
- Help with random number gen (C++)
Other Threads in the C++ Forum
- Previous Thread: prime numbers output. while loop inside a do-while loop having trouble
- Next Thread: C++ Stack Assignment; Compiles but doesn't quite work
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






