Apollo 11.0
自动驾驶开放平台
i_rand.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
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
17#pragma once
18
20
21namespace apollo {
22namespace perception {
23namespace algorithm {
24const int I_DEFAULT_SEED = 432;
25
26// Generate random number between 0 and 1, the seed s should not be 0
27inline double IRandCoreD(int *s) {
28 int a;
29 a = *s / 127773;
30 *s = 16807 * (*s - a * 127773) - 2836 * a;
31 if (*s < 0) {
32 *s += 2147483647;
33 }
34 return ((1.0 / (static_cast<double>(2147483647))) * (*s));
35}
36
37// Generate random integer in half-open interval [0,pool_size), the seed s
38// should not be 0
39inline int IRandI(int pool_size, int *s) {
40 return (IIntervalHalfopen<int>(static_cast<int>(IRandCoreD(s) * pool_size), 0,
41 pool_size));
42}
43
44// Generate n random integers from half open interval [0,pool_size)
45// note that if pool_size is smaller than sample Size n, the sample will contain
46// duplicate integers
47inline void IRandomSample(int *sample, int n, int pool_size, int *s) {
48 int temp, curr;
49 if (pool_size < n) {
50 for (int i = 0; i < n; i++) {
51 sample[i] = IRandI(pool_size, s);
52 }
53 } else {
54 for (int i = 0; i < n; i++) {
55 // generate integer for an integer pool that descreases with i
56 temp = IRandI(pool_size - i, s);
57 // go through previous storted integers, if smaller, store, and move the
58 // rest of the array one step. if larger or equal, increase by 1 and go
59 // to the next. This ensures even distribution
60 for (int j = 0; j < i; j++) {
61 curr = sample[j];
62 if (temp < curr) {
63 sample[j] = temp;
64 temp = curr;
65 } else {
66 temp++;
67 }
68 }
69 // store the final integer
70 sample[i] = temp;
71 }
72 }
73}
74
75// Generate a random permutation of array elements in place - Fisher and Yates
76// algorithm Array A has n elements, each element has Size l
77template <typename T>
78inline void IRandomizedShuffle(T *A, int n, int l, int *s) {
79 if (A == reinterpret_cast<T *>(NULL) || n <= 1 || l < 1) {
80 return;
81 }
82 int i, r;
83 for (i = n - 1; i > 0; i--) {
84 r = IRandI(i + 1, s); // pick a random index from 0 to i
85 ISwap(A + r * l, A + i * l, l);
86 }
87}
88
89// Generate a random permutation of two corresponding array elements
90// in place - Fisher and Yates algorithm
91// Array A and B has n elements, A's element has size la,
92// B's element has size lb
93template <typename T>
94inline void IRandomizedShuffle(T *A, T *B, int n, int la, int lb, int *s) {
95 if (A == reinterpret_cast<T *>(NULL) || B == reinterpret_cast<T *>(NULL) ||
96 n <= 1 || la < 1 || lb < 1) {
97 return;
98 }
99 int i, r;
100 for (i = n - 1; i > 0; i--) {
101 r = IRandI(i + 1, s); /*pick a random index from 0 to i*/
102 ISwap(A + r * la, A + i * la, la);
103 ISwap(B + r * lb, B + i * lb, lb);
104 }
105}
106
107// Generate a random permutation of array elements in place - Fisher and Yates
108// algorithm Array A has n elements, each element has Size 1
109template <typename T>
110inline void IRandomizedShuffle1(T *A, int n, int *s) {
111 if (A == reinterpret_cast<T *>(NULL) || n <= 1) {
112 return;
113 }
114 int i, r;
115 for (i = n - 1; i > 0; i--) {
116 // pick a random index from 0 to i
117 r = IRandI(i + 1, s);
118 ISwap(A[r], A[i]);
119 }
120}
121
122} // namespace algorithm
123} // namespace perception
124} // namespace apollo
void IRandomizedShuffle1(T *A, int n, int *s)
Definition i_rand.h:110
void IRandomizedShuffle(T *A, int n, int l, int *s)
Definition i_rand.h:78
double IRandCoreD(int *s)
Definition i_rand.h:27
int IRandI(int pool_size, int *s)
Definition i_rand.h:39
void ISwap(T &a, T &b)
Definition i_basic.h:299
void IRandomSample(int *sample, int n, int pool_size, int *s)
Definition i_rand.h:47
class register implement
Definition arena_queue.h:37