ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
random.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5  * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6  *
7  * THE BSD LICENSE
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *************************************************************************/
30 
31 #ifndef FLANN_RANDOM_H
32 #define FLANN_RANDOM_H
33 
34 #include <algorithm>
35 #include <random>
36 #include <cstdlib>
37 #include <cstddef>
38 #include <vector>
39 
40 #include "FLANN/general.h"
41 
42 namespace flann
43 {
44 
49 inline void seed_random(unsigned int seed)
50 {
51  srand(seed);
52 }
53 
54 /*
55  * Generates a random double value.
56  */
63 inline double rand_double(double high = 1.0, double low = 0)
64 {
65  return low + ((high-low) * (std::rand() / (RAND_MAX + 1.0)));
66 }
67 
74 inline int rand_int(int high = RAND_MAX, int low = 0)
75 {
76  return low + (int) ( double(high-low) * (std::rand() / (RAND_MAX + 1.0)));
77 }
78 
79 
81 {
82 public:
83  ptrdiff_t operator() (ptrdiff_t i) { return rand_int(i); }
84 };
85 
86 
92 {
93  std::vector<int> vals_;
94  int size_;
95  int counter_;
96 
97 public:
104  {
105  init(n);
106  }
107 
112  void init(int n)
113  {
114  // create and initialize an array of size n
115  vals_.resize(n);
116  size_ = n;
117  for (int i = 0; i < size_; ++i) vals_[i] = i;
118 
119  // shuffle the elements in the array
120  std::random_device rd; // non-deterministic generator
121  std::mt19937 gen(rd()); // to seed mersenne twister.
122  std::shuffle(vals_.begin(), vals_.end(), rd);
123 
124  counter_ = 0;
125  }
126 
132  int next()
133  {
134  if (counter_ == size_) {
135  return -1;
136  }
137  else {
138  return vals_[counter_++];
139  }
140  }
141 };
142 
143 }
144 
145 #endif //FLANN_RANDOM_H
146 
147 
int64_t size_
Definition: FilePLY.cpp:37
ptrdiff_t operator()(ptrdiff_t i)
Definition: random.h:83
void init(int n)
Definition: random.h:112
UniqueRandom(int n)
Definition: random.h:103
double rand_double(double high=1.0, double low=0)
Definition: random.h:63
void seed_random(unsigned int seed)
Definition: random.h:49
int rand_int(int high=RAND_MAX, int low=0)
Definition: random.h:74