Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  randomizer
  4. //
  5. //  Created by Brandon on 8/16/16.
  6. //  Copyright © 2016 Shogun3D. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <map>
  11. #include <vector>
  12. #include <time.h>
  13.  
  14.  
  15. /*
  16.  * Name: get_unique_number_count
  17.  * Desc: Returns the number of unique numbers generated from 100k numbers
  18.  *       between 1 and 500k.
  19.  */
  20. int get_unique_number_count()
  21. {
  22.     // Seed the random number generator
  23.     srand( (unsigned) time( NULL ) );
  24.    
  25.     // Hash map used to store random numbers
  26.     std::map<int,int> numbers;
  27.     int unique_numbers = 0;
  28.    
  29.     // Generate 100k random numbers and store their occurrences in the map
  30.     for( int i = 0; i < 100000; i++ )
  31.     {
  32.         // Generate a number between 1 and 500000
  33.         int n = 1+(rand()%499999);
  34.        
  35.         // Update the number of occurences of this particular number in the map
  36.         // then save the number of the previous value to minimize the need to read
  37.         // the hash map twice.
  38.         int occurence = numbers[n]++;
  39.        
  40.         // If this number was never added to the list before, the value should be
  41.         // zero and initially count it as a unique number.  If it was added before,
  42.         // the value should be one as it was already counted, then subtract from the
  43.         // number of unique numbers, essentially disqualifying it.
  44.         if( occurence == 0 )
  45.             unique_numbers++;
  46.         else if( occurence == 1 )
  47.             unique_numbers--;
  48.     }
  49.    
  50.     return unique_numbers;
  51. }
  52.  
  53.  
  54. /*
  55.  * Name: get_most_frequent_numbers
  56.  * Desc: Returns the list of numbers most frequently generated out of
  57.  *       100k numbers between 1 and 500k.
  58.  */
  59. void get_most_frequent_numbers( std::vector<int>& most_frequent )
  60. {
  61.     // Seed the random number generator
  62.     srand( (unsigned) time( NULL ) );
  63.    
  64.     // Hash map used to store random numbers
  65.     std::map<int,int> numbers;
  66.     int highest_occurence = 2;
  67.    
  68.     // Generate 100k random numbers and store their occurrences in the map
  69.     for( int i = 0; i < 100000; i++ )
  70.     {
  71.         // Generate a number between 1 and 500000
  72.         int n = 1+(rand()%499999);
  73.        
  74.         // Update the number of occurences of this particular number in the map
  75.         // then save the number of the previous value to minimize the need to read
  76.         // the hash map twice.
  77.         int occurence = ++numbers[n];
  78.        
  79.         // Save the number that has the highest occurence.  If the previous number has
  80.         // been surpassed, then empty the vector of the previous number(s) and give
  81.         // the new number priority.
  82.         if( occurence > highest_occurence )
  83.         {
  84.             highest_occurence = occurence;
  85.             most_frequent.clear();
  86.             most_frequent.push_back(n);
  87.         }
  88.         // If this number is tied with the number of highest occurence, save it also.
  89.         else if( occurence == highest_occurence )
  90.         {
  91.             most_frequent.push_back(n);
  92.         }
  93.     }
  94. }
  95.  
  96. int main(int argc, const char * argv[]) {
  97.     std::vector<int> mf;
  98.     int un = get_unique_number_count();
  99.     get_most_frequent_numbers( mf );
  100.    
  101.     std::cout << "Unique numbers: " << un << " out of 100000" << std::endl;
  102.     std::cout << "Most frequent numbers: ";
  103.    
  104.     std::vector<int>::iterator i = mf.begin();
  105.     while( i != mf.end() )
  106.     {
  107.         std::cout << (*i) << " ";
  108.         ++i;
  109.     }
  110.    
  111.     std::cout << std::endl;
  112.    
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement