• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "utils.h"
17 
18 #include <cstdlib>
19 #include <ctime>
20 #include <limits>
21 #include <random>
22 #include <unordered_set>
23 
24 namespace android {
25 namespace os {
26 namespace statsd {
27 
generateRandomIds(int count,int maxRange)28 std::vector<int> generateRandomIds(int count, int maxRange) {
29     std::srand(std::time(nullptr));
30 
31     std::unordered_set<int> unique_values;
32 
33     while (unique_values.size() <= count) {
34         unique_values.insert(std::rand() % maxRange);
35     }
36 
37     std::vector<int> result(unique_values.begin(), unique_values.end());
38 
39     return result;
40 }
41 
generateRandomHashIds(int count)42 std::vector<int64_t> generateRandomHashIds(int count) {
43     std::srand(std::time(nullptr));
44 
45     std::random_device rd;
46     std::mt19937_64 eng(rd());
47 
48     std::uniform_int_distribution<int64_t> distr;
49 
50     std::unordered_set<int> unique_values;
51 
52     while (unique_values.size() <= count) {
53         unique_values.insert(distr(eng));
54     }
55 
56     std::vector<int64_t> result(unique_values.begin(), unique_values.end());
57 
58     return result;
59 }
60 
61 }  // namespace statsd
62 }  // namespace os
63 }  // namespace android