Counting the frequency of random numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 4
Reputation: Armfelt is an unknown quantity at this point 
Solved Threads: 0
Armfelt's Avatar
Armfelt Armfelt is offline Offline
Newbie Poster

Counting the frequency of random numbers

 
0
  #1
Apr 21st, 2007
Hello everyone, this is my first post.
As can be quite rightly guessed, I am new to C++ and struggling with some coursework.

My task is to produce a program that prints out 5000 random numbers between 1-5, also printing out the frequency of each given number.

I have searched for an answer as to how this might be done, and I believe it might have something to do with Arrays, could someone please give me a hint whether this is right?

Here is my code so far
  1. #include <vcl.h>
  2. #include <iostream.h>
  3.  
  4. using namespace std;
  5.  
  6. int main(void)
  7. {
  8. int random_integer;
  9. for(int numero=0; numero < 5000; numero++){
  10. random_integer = 0 + int(6 * rand()/(RAND_MAX+1.0));
  11. cout << random_integer << endl;
  12. }
  13. cin.get();
  14. return 0;
  15. }

I realize that it is currently printing out only the random numbers...
Last edited by Armfelt; Apr 21st, 2007 at 1:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,616
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: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Counting the frequency of random numbers

 
0
  #2
Apr 21st, 2007
You would be required to have an array of the size the same as the range of numbers to be generated. For eg. as in your case, the range is 5, hence create an array of 5 elements, and for each random number generated, do something like:

  1. int frequencyArray [RANGE] = { 0 };
  2. random_integer = int(6 * rand()/(RAND_MAX+1.0));
  3.  
  4. // asssuming that the generated number is between 1 and 5.
  5. ++frequencyArray [randomNumber - 1];

BTW, why the need to add zero to your random number equation, which I think would also generate 0 which you don't need.

Consider doing:
  1. srand ( static_cast<unsigned int> ( time ( 0 ) ) );
  2. random_integer = 1 + int(5 * rand()/(RAND_MAX+1.0));

Last edited by ~s.o.s~; Apr 21st, 2007 at 1:30 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: Armfelt is an unknown quantity at this point 
Solved Threads: 0
Armfelt's Avatar
Armfelt Armfelt is offline Offline
Newbie Poster

Re: Counting the frequency of random numbers

 
0
  #3
Apr 21st, 2007
I managed to figure it out. The code is sans Arrays due to the realization that I am not supposed to know how to use them yet. Sincere thanks for the help ~s.o.s~. For posterity, here is the code:


  1. #include <vcl>
  2. #include <iostream>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int main(void)
  8. {
  9. srand((unsigned)time(0));
  10. int random_integer;
  11. int number1 = 0;
  12. int number2 = 0;
  13. int number3 = 0;
  14. int number4 = 0;
  15. int number5 = 0;
  16. int lowest=1, highest=5;
  17. int size=(highest-lowest)+1;
  18.  
  19. for(int n=0; n < 5000; n++)
  20. {
  21. random_integer = alin+int(koko*rand()/(RAND_MAX+ 1.0));
  22. cout << random_integer << endl;
  23. if (random_integer == 1) numero1++;
  24. else if (random_integer == 2) numero2++;
  25. else if (random_integer == 3) numero3++;
  26. else if (random_integer == 4) numero4++;
  27. else if (random_integer == 5) numero5++;
  28. }
  29. cout << random_integer << endl;
  30. cout << "\n1 = "<<number1<<""<<endl;
  31. cout << "2 = "<<number2<<""<<endl;
  32. cout << "3 = "<<number3<<""<<endl;
  33. cout << "4 = "<<number4<<""<<endl;
  34. cout << "5 = "<<number5<<""<<endl;
  35.  
  36. cin.get();
  37. return 0;
  38. }

I presume that the the aesthetics of my code do not appeal to patrons
Last edited by Armfelt; Apr 21st, 2007 at 3:11 pm. Reason: code indenting enhancements & minor tweaks..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Counting the frequency of random numbers

 
0
  #4
Apr 21st, 2007
>I presume that the the aesthetics of my code do not appeal to patrons

No but your avatar does

>#include <iostream.h>
No it should be just iostream.

Don't forget to align your braces and properly indent your code.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: Armfelt is an unknown quantity at this point 
Solved Threads: 0
Armfelt's Avatar
Armfelt Armfelt is offline Offline
Newbie Poster

Re: Counting the frequency of random numbers

 
0
  #5
Apr 21st, 2007
Thanks for the pointers (and praise for my avatar ) I edited the code accordingly.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Counting the frequency of random numbers

 
0
  #6
Apr 21st, 2007
  1. #include <vcl>
  2. #include <iostream>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int main ( void )
  8. {
  9. srand ( ( unsigned ) time ( 0 ) );
  10. int random_integer;
  11. int number1 = 0;
  12. int number2 = 0;
  13. int number3 = 0;
  14. int number4 = 0;
  15. int number5 = 0;
  16. int lowest = 1, highest = 5;
  17. int size = ( highest - lowest ) + 1;
  18.  
  19. for ( int n = 0; n < 5000; n++ )
  20. {
  21. random_integer = alin + int ( koko * rand() / ( RAND_MAX + 1.0 ) );
  22. cout << random_integer << endl;
  23. if ( random_integer == 1 )
  24. numero1++;
  25. else if ( random_integer == 2 )
  26. numero2++;
  27. else if ( random_integer == 3 )
  28. numero3++;
  29. else if ( random_integer == 4 )
  30. numero4++;
  31. else if ( random_integer == 5 )
  32. numero5++;
  33. }
  34. cout << random_integer << endl;
  35. cout << "\n1 = " << number1 << "" << endl;
  36. cout << "2 = " << number2 << "" << endl;
  37. cout << "3 = " << number3 << "" << endl;
  38. cout << "4 = " << number4 << "" << endl;
  39. cout << "5 = " << number5 << "" << endl;
  40.  
  41. cin.get();
  42. return 0;
  43. }

Well I might be tempted to do something like the above.

Also does cout << "\n1 = " << number1 << "" << endl; have any purpose?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: Armfelt is an unknown quantity at this point 
Solved Threads: 0
Armfelt's Avatar
Armfelt Armfelt is offline Offline
Newbie Poster

Re: Counting the frequency of random numbers

 
0
  #7
Apr 21st, 2007
Also does cout << "\n1 = " << number1 << "" << endl; have any purpose?
That bit was left over from an a planned text output thingy. Good catch.

Let me guess: you ran the code through a 'beautifier' program of some sort? Or did you blaze through it pressing space . I must agree it looks better that way. Case closed!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Counting the frequency of random numbers

 
0
  #8
Apr 21st, 2007
Yes something like that, enjoy your stay at daniweb.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC