C++ Random Number Generator

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 2
Reputation: PennKen2009 is an unknown quantity at this point 
Solved Threads: 0
PennKen2009 PennKen2009 is offline Offline
Newbie Poster

C++ Random Number Generator

 
0
  #1
Jan 29th, 2007
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:

  1. int main()
  2. {
  3. srand((unsigned)time(0));
  4. int a;
  5. int lowest=1, highest=19;
  6. int range=(highest-lowest)+1;
  7. for(int index=0; index<1; index++){
  8. a = lowest+int(range*rand()/(RAND_MAX + 1.0));
  9. cout << a << endl;
  10. }
  11.  
  12. }
Last edited by ~s.o.s~; Jan 29th, 2007 at 12:53 pm. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: C++ Random Number Generator

 
0
  #2
Jan 29th, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ Random Number Generator

 
0
  #3
Jan 29th, 2007
Another confusing request...
Originally Posted by PennKen2009 View Post
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:
Originally Posted by PennKen2009 View Post
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);
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 4
Reputation: rajeevbhatt17 is an unknown quantity at this point 
Solved Threads: 0
rajeevbhatt17 rajeevbhatt17 is offline Offline
Newbie Poster

Re: C++ Random Number Generator

 
0
  #4
Aug 23rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: C++ Random Number Generator

 
0
  #5
Aug 23rd, 2007
> 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).
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 4
Reputation: rajeevbhatt17 is an unknown quantity at this point 
Solved Threads: 0
rajeevbhatt17 rajeevbhatt17 is offline Offline
Newbie Poster

Re: C++ Random Number Generator

 
0
  #6
Aug 27th, 2007
Originally Posted by vijayan121 View Post
> 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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: C++ Random Number Generator

 
0
  #7
Aug 28th, 2007
>> 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.
Last edited by vijayan121; Aug 28th, 2007 at 10:51 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: zati is an unknown quantity at this point 
Solved Threads: 0
zati zati is offline Offline
Newbie Poster
 
-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; }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC