this code generate 100 random number between 1 100.
#include <cstdlib> #include <ctime> #include <iostream> using namespace std; int main() { srand(time(null)); (int = 0; < 100; ++i) { cout << rand() % 100 + 1 << "\t"; //number between 1 , 100 if ((i+1) % 5 == 0) cout << endl; } return 0; }
i want add function in code count how many times number repeated in output. example in result-
56 96 75 53 67 89 23 26 27 99 30 29 2 63 27 32 49 18 79 16 56 31 95 46 82 30 63 68 68 100 7 23 95 81 75 13 21 97 39 99 48 68 28 49 83 6 32 31 23 11 98 30 93 93 76 74 74 38 42 41 37 48 16 84 81 90 48 1 39 86 52 86 6 79 86 88 36 17 70 11 27 19 40 71 63 67 45 37 56 38 29 45 85 44 80 17 86 27 70 76
the number 86 repeated 3 times
the number 56 repeated 3 times
the number 89 repeated 1 times ... etc.
how can it?
something that... nb: tried take account comment
#include <cstdlib> #include <ctime> #include <iostream> #include <map> #include <algorithm> int main() { srand(time(null)); std::map<int,int> histo; int n = 1 , m = 100; (int = 0; < m; ++i) { int rnd = m + rand() / (rand_max / (n - m + 1) + 1); histo[rnd] += 1; std::cout << rnd << std::endl; if ((i+1) % 5 == 0) std::cout << std::endl; } for(auto var = begin(histo) ; var != end(histo) ; ++var) { std::cout << var->first << " have " << var->second << " occurence " << std::endl; } return 0; }
Comments
Post a Comment