NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
RNG.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include <random>
32 
34 #include "NumCpp/Random/beta.hpp"
36 #include "NumCpp/Random/cauchy.hpp"
38 #include "NumCpp/Random/choice.hpp"
42 #include "NumCpp/Random/f.hpp"
43 #include "NumCpp/Random/gamma.hpp"
49 #include "NumCpp/Random/normal.hpp"
52 #include "NumCpp/Random/rand.hpp"
55 #include "NumCpp/Random/randN.hpp"
63 
64 namespace nc
65 {
66  namespace random
67  {
68  //============================================================================
69  // Class Description:
72  template<typename GeneratorType = std::mt19937_64>
73  class RNG
74  {
75  public:
76  //============================================================================
77  // Method Description:
80  RNG() = default;
81 
82  //============================================================================
83  // Method Description:
88  explicit RNG(int seed) :
89  generator_(seed){};
90 
91  //============================================================================
92  // Method Description:
98  bool bernoulli(double inP = 0.5)
99  {
100  return detail::bernoulli(generator_, inP);
101  }
102 
103  //============================================================================
104  // Method Description:
112  NdArray<bool> bernoulli(const Shape& inShape, double inP = 0.5)
113  {
114  return detail::bernoulli(generator_, inShape, inP);
115  }
116 
117 #ifndef NUMCPP_NO_USE_BOOST
118  //============================================================================
119  // Method Description:
130  template<typename dtype>
131  dtype beta(dtype inAlpha, dtype inBeta)
132  {
133  return detail::beta(generator_, inAlpha, inBeta);
134  }
135 
136  //============================================================================
137  // Method Description:
150  template<typename dtype>
151  NdArray<dtype> beta(const Shape& inShape, dtype inAlpha, dtype inBeta)
152  {
153  return detail::beta(generator_, inShape, inAlpha, inBeta);
154  }
155 #endif
156 
157  //============================================================================
158  // Method Description:
168  template<typename dtype>
169  dtype binomial(dtype inN, double inP = 0.5)
170  {
171  return detail::binomial(generator_, inN, inP);
172  }
173 
174  //============================================================================
175  // Method Description:
187  template<typename dtype>
188  NdArray<dtype> binomial(const Shape& inShape, dtype inN, double inP = 0.5)
189  {
190  return detail::binomial(generator_, inShape, inN, inP);
191  }
192 
193  //============================================================================
194  // Method Description:
202  template<typename dtype>
203  dtype cauchy(dtype inMean = 0, dtype inSigma = 1)
204  {
205  return detail::cauchy(generator_, inMean, inSigma);
206  }
207 
208  //============================================================================
209  // Method Description:
219  template<typename dtype>
220  NdArray<dtype> cauchy(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
221  {
222  return detail::cauchy(generator_, inShape, inMean, inSigma);
223  }
224 
225  //============================================================================
226  // Method Description:
235  template<typename dtype>
236  dtype chiSquare(dtype inDof)
237  {
238  return detail::chiSquare(generator_, inDof);
239  }
240 
241  //============================================================================
242  // Method Description:
253  template<typename dtype>
254  NdArray<dtype> chiSquare(const Shape& inShape, dtype inDof)
255  {
256  return detail::chiSquare(generator_, inShape, inDof);
257  }
258 
259  //============================================================================
260  // Method Description:
266  template<typename dtype>
267  dtype choice(const NdArray<dtype>& inArray)
268  {
269  return detail::choice(generator_, inArray);
270  }
271 
272  //============================================================================
273  // Method Description:
281  template<typename dtype>
282  NdArray<dtype> choice(const NdArray<dtype>& inArray, uint32 inNum, bool replace = true)
283  {
284  return detail::choice(generator_, inArray, inNum, replace);
285  }
286 
287  //============================================================================
288  // Method Description:
297  template<typename dtype>
298  dtype discrete(const NdArray<double>& inWeights)
299  {
300  return detail::discrete<dtype>(generator_, inWeights);
301  }
302 
303  //============================================================================
304  // Method Description:
315  template<typename dtype>
316  NdArray<dtype> discrete(const Shape& inShape, const NdArray<double>& inWeights)
317  {
318  return detail::discrete<dtype>(generator_, inShape, inWeights);
319  }
320 
321  //============================================================================
322  // Method Description:
331  template<typename dtype>
332  dtype exponential(dtype inScaleValue = 1)
333  {
334  return detail::exponential(generator_, inScaleValue);
335  }
336 
337  //============================================================================
338  // Method Description:
349  template<typename dtype>
350  NdArray<dtype> exponential(const Shape& inShape, dtype inScaleValue = 1)
351  {
352  return detail::exponential(generator_, inShape, inScaleValue);
353  }
354 
355  //============================================================================
356  // Method Description:
363  template<typename dtype>
364  dtype extremeValue(dtype inA = 1, dtype inB = 1)
365  {
366  return detail::extremeValue(generator_, inA, inB);
367  }
368 
369  //============================================================================
370  // Method Description:
379  template<typename dtype>
380  NdArray<dtype> extremeValue(const Shape& inShape, dtype inA = 1, dtype inB = 1)
381  {
382  return detail::extremeValue(generator_, inShape, inA, inB);
383  }
384 
385  //============================================================================
386  // Method Description:
395  template<typename dtype>
396  dtype f(dtype inDofN, dtype inDofD)
397  {
398  return detail::f(generator_, inDofN, inDofD);
399  }
400 
401  //============================================================================
402  // Method Description:
413  template<typename dtype>
414  NdArray<dtype> f(const Shape& inShape, dtype inDofN, dtype inDofD)
415  {
416  return detail::f(generator_, inShape, inDofN, inDofD);
417  }
418 
419  //============================================================================
420  // Method Description:
430  template<typename dtype>
431  dtype gamma(dtype inGammaShape, dtype inScaleValue = 1)
432  {
433  return detail::gamma(generator_, inGammaShape, inScaleValue);
434  }
435 
436  //============================================================================
437  // Method Description:
449  template<typename dtype>
450  NdArray<dtype> gamma(const Shape& inShape, dtype inGammaShape, dtype inScaleValue = 1)
451  {
452  return detail::gamma(generator_, inShape, inGammaShape, inScaleValue);
453  }
454 
455  //============================================================================
456  // Method Description:
465  template<typename dtype>
466  dtype geometric(double inP = 0.5)
467  {
468  return detail::geometric<dtype>(generator_, inP);
469  }
470 
471  //============================================================================
472  // Method Description:
483  template<typename dtype>
484  NdArray<dtype> geometric(const Shape& inShape, double inP = 0.5)
485  {
486  return detail::geometric<dtype>(generator_, inShape, inP);
487  }
488 
489 #ifndef NUMCPP_NO_USE_BOOST
490  //============================================================================
491  // Method Description:
502  template<typename dtype>
503  dtype laplace(dtype inLoc = 0, dtype inScale = 1)
504  {
505  return detail::laplace(generator_, inLoc, inScale);
506  }
507 
508  //============================================================================
509  // Method Description:
522  template<typename dtype>
523  NdArray<dtype> laplace(const Shape& inShape, dtype inLoc = 0, dtype inScale = 1)
524  {
525  return detail::laplace(generator_, inShape, inLoc, inScale);
526  }
527 #endif
528 
529  //============================================================================
530  // Method Description:
541  template<typename dtype>
542  dtype lognormal(dtype inMean = 0, dtype inSigma = 1)
543  {
544  return detail::lognormal(generator_, inMean, inSigma);
545  }
546 
547  //============================================================================
548  // Method Description:
561  template<typename dtype>
562  NdArray<dtype> lognormal(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
563  {
564  return detail::lognormal(generator_, inShape, inMean, inSigma);
565  }
566 
567  //============================================================================
568  // Method Description:
578  template<typename dtype>
579  dtype negativeBinomial(dtype inN, double inP = 0.5)
580  {
581  return detail::negativeBinomial(generator_, inN, inP);
582  }
583 
584  //============================================================================
585  // Method Description:
597  template<typename dtype>
598  NdArray<dtype> negativeBinomial(const Shape& inShape, dtype inN, double inP = 0.5)
599  {
600  return detail::negativeBinomial(generator_, inShape, inN, inP);
601  }
602 
603 #ifndef NUMCPP_NO_USE_BOOST
604  //============================================================================
605  // Method Description:
616  template<typename dtype>
617  dtype nonCentralChiSquared(dtype inK = 1, dtype inLambda = 1)
618  {
619  return detail::nonCentralChiSquared(generator_, inK, inLambda);
620  }
621 
622  //============================================================================
623  // Method Description:
636  template<typename dtype>
637  NdArray<dtype> nonCentralChiSquared(const Shape& inShape, dtype inK = 1, dtype inLambda = 1)
638  {
639  return detail::nonCentralChiSquared(generator_, inShape, inK, inLambda);
640  }
641 #endif
642 
643  //============================================================================
644  // Method Description:
655  template<typename dtype>
656  dtype normal(dtype inMean = 0, dtype inSigma = 1)
657  {
658  return detail::normal(generator_, inMean, inSigma);
659  }
660 
661  //============================================================================
662  // Method Description:
675  template<typename dtype>
676  NdArray<dtype> normal(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
677  {
678  return detail::normal(generator_, inShape, inMean, inSigma);
679  }
680 
681  //============================================================================
682  // Method Description:
690  template<typename dtype>
692  {
693  return detail::permutation(generator_, inValue);
694  }
695 
696  //============================================================================
697  // Method Description:
705  template<typename dtype>
707  {
708  return detail::permutation(generator_, inArray);
709  }
710 
711  //============================================================================
712  // Method Description:
721  template<typename dtype>
722  dtype poisson(double inMean = 1)
723  {
724  return detail::poisson<dtype>(generator_, inMean);
725  }
726 
727  //============================================================================
728  // Method Description:
739  template<typename dtype>
740  NdArray<dtype> poisson(const Shape& inShape, double inMean = 1)
741  {
742  return detail::poisson<dtype>(generator_, inShape, inMean);
743  }
744 
745  //============================================================================
746  // Method Description:
754  template<typename dtype>
755  dtype rand()
756  {
757  return detail::rand<dtype>(generator_);
758  }
759 
760  //============================================================================
761  // Method Description:
771  template<typename dtype>
772  NdArray<dtype> rand(const Shape& inShape)
773  {
774  return detail::rand<dtype>(generator_, inShape);
775  }
776 
777  //============================================================================
778  // Method Description:
790  template<typename dtype>
791  dtype randFloat(dtype inLow, dtype inHigh = 0.0)
792  {
793  return detail::randFloat(generator_, inLow, inHigh);
794  }
795 
796  //============================================================================
797  // Method Description:
810  template<typename dtype>
811  NdArray<dtype> randFloat(const Shape& inShape, dtype inLow, dtype inHigh = 0.0)
812  {
813  return detail::randFloat(generator_, inShape, inLow, inHigh);
814  }
815 
816  //============================================================================
817  // Method Description:
829  template<typename dtype>
830  dtype randInt(dtype inLow, dtype inHigh = 0)
831  {
832  return detail::randInt(generator_, inLow, inHigh);
833  }
834 
835  //============================================================================
836  // Method Description:
849  template<typename dtype>
850  NdArray<dtype> randInt(const Shape& inShape, dtype inLow, dtype inHigh = 0)
851  {
852  return detail::randInt(generator_, inShape, inLow, inHigh);
853  }
854 
855  //============================================================================
856  // Method Description:
864  template<typename dtype>
865  dtype randN()
866  {
867  return detail::randN<dtype>(generator_);
868  }
869 
870  //============================================================================
871  // Method Description:
881  template<typename dtype>
882  NdArray<dtype> randN(const Shape& inShape)
883  {
884  return detail::randN<dtype>(generator_, inShape);
885  }
886 
887  //============================================================================
888  // Method Description:
893  void seed(int value) noexcept
894  {
895  generator_.seed(value);
896  }
897 
898  //============================================================================
899  // Method Description:
904  template<typename dtype>
905  void shuffle(NdArray<dtype>& inArray)
906  {
907  return detail::shuffle(generator_, inArray);
908  }
909 
910  //============================================================================
911  // Method Description:
920  template<typename dtype>
922  {
923  return detail::standardNormal<dtype>(generator_);
924  }
925 
926  //============================================================================
927  // Method Description:
938  template<typename dtype>
940  {
941  return detail::standardNormal<dtype>(generator_, inShape);
942  }
943 
944  //============================================================================
945  // Method Description:
954  template<typename dtype>
955  dtype studentT(dtype inDof)
956  {
957  return detail::studentT(generator_, inDof);
958  }
959 
960  //============================================================================
961  // Method Description:
972  template<typename dtype>
973  NdArray<dtype> studentT(const Shape& inShape, dtype inDof)
974  {
975  return detail::studentT(generator_, inShape, inDof);
976  }
977 
978 #ifndef NUMCPP_NO_USE_BOOST
979  //============================================================================
980  // Method Description:
992  template<typename dtype>
993  dtype triangle(dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
994  {
995  return detail::triangle(generator_, inA, inB, inC);
996  }
997 
998  //============================================================================
999  // Method Description:
1013  template<typename dtype>
1014  NdArray<dtype> triangle(const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
1015  {
1016  return detail::triangle(generator_, inShape, inA, inB, inC);
1017  }
1018 #endif
1019 
1020  //============================================================================
1021  // Method Description:
1034  template<typename dtype>
1035  dtype uniform(dtype inLow, dtype inHigh)
1036  {
1037  return detail::uniform(generator_, inLow, inHigh);
1038  }
1039 
1040  //============================================================================
1041  // Method Description:
1055  template<typename dtype>
1056  NdArray<dtype> uniform(const Shape& inShape, dtype inLow, dtype inHigh)
1057  {
1058  return detail::uniform(generator_, inShape, inLow, inHigh);
1059  }
1060 
1061 #ifndef NUMCPP_NO_USE_BOOST
1062  //============================================================================
1063  // Method Description:
1072  template<typename dtype>
1073  NdArray<dtype> uniformOnSphere(uint32 inNumPoints, uint32 inDims = 2)
1074  {
1075  return detail::uniformOnSphere<dtype>(generator_, inNumPoints, inDims);
1076  }
1077 #endif
1078 
1079  //============================================================================
1080  // Method Description:
1090  template<typename dtype>
1091  dtype weibull(dtype inA = 1, dtype inB = 1)
1092  {
1093  return detail::weibull(generator_, inA, inB);
1094  }
1095 
1096  //============================================================================
1097  // Method Description:
1109  template<typename dtype>
1110  NdArray<dtype> weibull(const Shape& inShape, dtype inA = 1, dtype inB = 1)
1111  {
1112  return detail::weibull(generator_, inShape, inA, inB);
1113  }
1114 
1115  private:
1116  GeneratorType generator_{};
1117  };
1118  } // namespace random
1119 } // namespace nc
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
Definition: RNG.hpp:74
dtype normal(dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:656
dtype gamma(dtype inGammaShape, dtype inScaleValue=1)
Definition: RNG.hpp:431
NdArray< dtype > binomial(const Shape &inShape, dtype inN, double inP=0.5)
Definition: RNG.hpp:188
NdArray< dtype > randFloat(const Shape &inShape, dtype inLow, dtype inHigh=0.0)
Definition: RNG.hpp:811
bool bernoulli(double inP=0.5)
Definition: RNG.hpp:98
NdArray< dtype > standardNormal(const Shape &inShape)
Definition: RNG.hpp:939
dtype triangle(dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: RNG.hpp:993
NdArray< dtype > randN(const Shape &inShape)
Definition: RNG.hpp:882
dtype randInt(dtype inLow, dtype inHigh=0)
Definition: RNG.hpp:830
NdArray< dtype > choice(const NdArray< dtype > &inArray, uint32 inNum, bool replace=true)
Definition: RNG.hpp:282
NdArray< dtype > uniformOnSphere(uint32 inNumPoints, uint32 inDims=2)
Definition: RNG.hpp:1073
dtype choice(const NdArray< dtype > &inArray)
Definition: RNG.hpp:267
dtype beta(dtype inAlpha, dtype inBeta)
Definition: RNG.hpp:131
NdArray< dtype > uniform(const Shape &inShape, dtype inLow, dtype inHigh)
Definition: RNG.hpp:1056
dtype lognormal(dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:542
NdArray< dtype > beta(const Shape &inShape, dtype inAlpha, dtype inBeta)
Definition: RNG.hpp:151
dtype studentT(dtype inDof)
Definition: RNG.hpp:955
dtype negativeBinomial(dtype inN, double inP=0.5)
Definition: RNG.hpp:579
dtype rand()
Definition: RNG.hpp:755
dtype f(dtype inDofN, dtype inDofD)
Definition: RNG.hpp:396
NdArray< dtype > exponential(const Shape &inShape, dtype inScaleValue=1)
Definition: RNG.hpp:350
dtype randFloat(dtype inLow, dtype inHigh=0.0)
Definition: RNG.hpp:791
NdArray< bool > bernoulli(const Shape &inShape, double inP=0.5)
Definition: RNG.hpp:112
dtype binomial(dtype inN, double inP=0.5)
Definition: RNG.hpp:169
dtype randN()
Definition: RNG.hpp:865
RNG(int seed)
Definition: RNG.hpp:88
NdArray< dtype > poisson(const Shape &inShape, double inMean=1)
Definition: RNG.hpp:740
NdArray< dtype > lognormal(const Shape &inShape, dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:562
NdArray< dtype > normal(const Shape &inShape, dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:676
NdArray< dtype > geometric(const Shape &inShape, double inP=0.5)
Definition: RNG.hpp:484
NdArray< dtype > negativeBinomial(const Shape &inShape, dtype inN, double inP=0.5)
Definition: RNG.hpp:598
dtype geometric(double inP=0.5)
Definition: RNG.hpp:466
NdArray< dtype > randInt(const Shape &inShape, dtype inLow, dtype inHigh=0)
Definition: RNG.hpp:850
dtype chiSquare(dtype inDof)
Definition: RNG.hpp:236
dtype cauchy(dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:203
dtype exponential(dtype inScaleValue=1)
Definition: RNG.hpp:332
NdArray< dtype > gamma(const Shape &inShape, dtype inGammaShape, dtype inScaleValue=1)
Definition: RNG.hpp:450
dtype uniform(dtype inLow, dtype inHigh)
Definition: RNG.hpp:1035
NdArray< dtype > chiSquare(const Shape &inShape, dtype inDof)
Definition: RNG.hpp:254
NdArray< dtype > cauchy(const Shape &inShape, dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:220
void seed(int value) noexcept
Definition: RNG.hpp:893
dtype discrete(const NdArray< double > &inWeights)
Definition: RNG.hpp:298
dtype laplace(dtype inLoc=0, dtype inScale=1)
Definition: RNG.hpp:503
NdArray< dtype > permutation(dtype inValue)
Definition: RNG.hpp:691
dtype weibull(dtype inA=1, dtype inB=1)
Definition: RNG.hpp:1091
NdArray< dtype > studentT(const Shape &inShape, dtype inDof)
Definition: RNG.hpp:973
NdArray< dtype > triangle(const Shape &inShape, dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: RNG.hpp:1014
NdArray< dtype > extremeValue(const Shape &inShape, dtype inA=1, dtype inB=1)
Definition: RNG.hpp:380
NdArray< dtype > rand(const Shape &inShape)
Definition: RNG.hpp:772
dtype standardNormal()
Definition: RNG.hpp:921
void shuffle(NdArray< dtype > &inArray)
Definition: RNG.hpp:905
NdArray< dtype > discrete(const Shape &inShape, const NdArray< double > &inWeights)
Definition: RNG.hpp:316
NdArray< dtype > nonCentralChiSquared(const Shape &inShape, dtype inK=1, dtype inLambda=1)
Definition: RNG.hpp:637
dtype extremeValue(dtype inA=1, dtype inB=1)
Definition: RNG.hpp:364
dtype poisson(double inMean=1)
Definition: RNG.hpp:722
NdArray< dtype > f(const Shape &inShape, dtype inDofN, dtype inDofD)
Definition: RNG.hpp:414
NdArray< dtype > weibull(const Shape &inShape, dtype inA=1, dtype inB=1)
Definition: RNG.hpp:1110
dtype nonCentralChiSquared(dtype inK=1, dtype inLambda=1)
Definition: RNG.hpp:617
NdArray< dtype > laplace(const Shape &inShape, dtype inLoc=0, dtype inScale=1)
Definition: RNG.hpp:523
NdArray< dtype > permutation(const NdArray< dtype > &inArray)
Definition: RNG.hpp:706
dtype choice(GeneratorType &generator, const NdArray< dtype > &inArray)
Definition: choice.hpp:54
NdArray< dtype > permutation(GeneratorType &generator, dtype inValue)
Definition: permutation.hpp:55
dtype beta(GeneratorType &generator, dtype inAlpha, dtype inBeta)
Definition: Random/beta.hpp:63
dtype triangle(GeneratorType &generator, dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: triangle.hpp:65
dtype cauchy(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition: cauchy.hpp:57
dtype binomial(GeneratorType &generator, dtype inN, double inP=0.5)
Definition: binomial.hpp:59
dtype chiSquare(GeneratorType &generator, dtype inDof)
Definition: chiSquare.hpp:58
void shuffle(GeneratorType &generator, NdArray< dtype > &inArray)
Definition: shuffle.hpp:50
bool bernoulli(GeneratorType &generator, double inP=0.5)
Definition: Random/bernoulli.hpp:55
dtype randInt(GeneratorType &generator, dtype inLow, dtype inHigh=0)
Definition: randInt.hpp:63
dtype randFloat(GeneratorType &generator, dtype inLow, dtype inHigh=0.0)
Definition: randFloat.hpp:63
dtype weibull(GeneratorType &generator, dtype inA=1, dtype inB=1)
Definition: weibull.hpp:58
dtype nonCentralChiSquared(GeneratorType &generator, dtype inK=1, dtype inLambda=1)
Definition: nonCentralChiSquared.hpp:63
dtype laplace(GeneratorType &generator, dtype inLoc=0, dtype inScale=1)
Definition: Random/laplace.hpp:61
dtype normal(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition: normal.hpp:60
dtype uniform(GeneratorType &generator, dtype inLow, dtype inHigh)
Definition: uniform.hpp:56
dtype extremeValue(GeneratorType &generator, dtype inA=1, dtype inB=1)
Definition: extremeValue.hpp:56
dtype studentT(GeneratorType &generator, dtype inDof)
Definition: studentT.hpp:57
dtype gamma(GeneratorType &generator, dtype inGammaShape, dtype inScaleValue=1)
Definition: Random/gamma.hpp:59
dtype negativeBinomial(GeneratorType &generator, dtype inN, double inP=0.5)
Definition: negativeBinomial.hpp:59
dtype lognormal(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition: lognormal.hpp:60
dtype exponential(GeneratorType &generator, dtype inScaleValue=1)
Definition: exponential.hpp:56
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:58
Definition: Coordinate.hpp:45
NdArray< dtype > replace(const NdArray< dtype > &inArray, dtype oldValue, dtype newValue)
Definition: replace.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40