-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindUniformMax.cpp
More file actions
33 lines (30 loc) · 867 Bytes
/
Copy pathFindUniformMax.cpp
File metadata and controls
33 lines (30 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <vector>
#include <iostream>
#include <climits>
#include <random>
#include <algorithm>
#include <chrono>
using namespace std;
// obtain a time-based seed:
std::mt19937 rng;
std::random_device rd;
int findUniformMax(int *arr, int n){
vector<int>v;
int MAX = INT_MIN;
for (int i =0; i <n;i++) {
if(arr[i] >=MAX){
MAX = arr[i];
if(v.size() && arr[v[0]] < MAX)
v.clear();
v.emplace_back(i);
}
}
//rng.seed(rd());
rng.seed(std::chrono::high_resolution_clock::now().time_since_epoch().count());
std::uniform_int_distribution<std::mt19937::result_type> dist(0,v.size()-1);
return v[dist(rng)];
}
int main() {
int arr[] = { 1,3, 6 ,4, 8, 7, 8, 8, 8, 8, 8, 5};
cout <<findUniformMax(arr, sizeof(arr)/sizeof(arr[0]));
}