NumCpp  2.11.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::random
65 {
66  //============================================================================
67  // Class Description:
70  template<typename GeneratorType = std::mt19937_64>
71  class RNG
72  {
73  public:
74  //============================================================================
75  // Method Description:
78  RNG() = default;
79 
80  //============================================================================
81  // Method Description:
86  explicit RNG(int seed) :
87  generator_(seed){};
88 
89  //============================================================================
90  // Method Description:
96  bool bernoulli(double inP = 0.5)
97  {
98  return detail::bernoulli(generator_, inP);
99  }
100 
101  //============================================================================
102  // Method Description:
110  NdArray<bool> bernoulli(const Shape& inShape, double inP = 0.5)
111  {
112  return detail::bernoulli(generator_, inShape, inP);
113  }
114 
115 #ifndef NUMCPP_NO_USE_BOOST
116  //============================================================================
117  // Method Description:
128  template<typename dtype>
129  dtype beta(dtype inAlpha, dtype inBeta)
130  {
131  return detail::beta(generator_, inAlpha, inBeta);
132  }
133 
134  //============================================================================
135  // Method Description:
148  template<typename dtype>
149  NdArray<dtype> beta(const Shape& inShape, dtype inAlpha, dtype inBeta)
150  {
151  return detail::beta(generator_, inShape, inAlpha, inBeta);
152  }
153 #endif
154 
155  //============================================================================
156  // Method Description:
166  template<typename dtype>
167  dtype binomial(dtype inN, double inP = 0.5)
168  {
169  return detail::binomial(generator_, inN, inP);
170  }
171 
172  //============================================================================
173  // Method Description:
185  template<typename dtype>
186  NdArray<dtype> binomial(const Shape& inShape, dtype inN, double inP = 0.5)
187  {
188  return detail::binomial(generator_, inShape, inN, inP);
189  }
190 
191  //============================================================================
192  // Method Description:
200  template<typename dtype>
201  dtype cauchy(dtype inMean = 0, dtype inSigma = 1)
202  {
203  return detail::cauchy(generator_, inMean, inSigma);
204  }
205 
206  //============================================================================
207  // Method Description:
217  template<typename dtype>
218  NdArray<dtype> cauchy(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
219  {
220  return detail::cauchy(generator_, inShape, inMean, inSigma);
221  }
222 
223  //============================================================================
224  // Method Description:
233  template<typename dtype>
234  dtype chiSquare(dtype inDof)
235  {
236  return detail::chiSquare(generator_, inDof);
237  }
238 
239  //============================================================================
240  // Method Description:
251  template<typename dtype>
252  NdArray<dtype> chiSquare(const Shape& inShape, dtype inDof)
253  {
254  return detail::chiSquare(generator_, inShape, inDof);
255  }
256 
257  //============================================================================
258  // Method Description:
264  template<typename dtype>
265  dtype choice(const NdArray<dtype>& inArray)
266  {
267  return detail::choice(generator_, inArray);
268  }
269 
270  //============================================================================
271  // Method Description:
279  template<typename dtype>
280  NdArray<dtype> choice(const NdArray<dtype>& inArray, uint32 inNum, bool replace = true)
281  {
282  return detail::choice(generator_, inArray, inNum, replace);
283  }
284 
285  //============================================================================
286  // Method Description:
295  template<typename dtype>
296  dtype discrete(const NdArray<double>& inWeights)
297  {
298  return detail::discrete<dtype>(generator_, inWeights);
299  }
300 
301  //============================================================================
302  // Method Description:
313  template<typename dtype>
314  NdArray<dtype> discrete(const Shape& inShape, const NdArray<double>& inWeights)
315  {
316  return detail::discrete<dtype>(generator_, inShape, inWeights);
317  }
318 
319  //============================================================================
320  // Method Description:
329  template<typename dtype>
330  dtype exponential(dtype inScaleValue = 1)
331  {
332  return detail::exponential(generator_, inScaleValue);
333  }
334 
335  //============================================================================
336  // Method Description:
347  template<typename dtype>
348  NdArray<dtype> exponential(const Shape& inShape, dtype inScaleValue = 1)
349  {
350  return detail::exponential(generator_, inShape, inScaleValue);
351  }
352 
353  //============================================================================
354  // Method Description:
361  template<typename dtype>
362  dtype extremeValue(dtype inA = 1, dtype inB = 1)
363  {
364  return detail::extremeValue(generator_, inA, inB);
365  }
366 
367  //============================================================================
368  // Method Description:
377  template<typename dtype>
378  NdArray<dtype> extremeValue(const Shape& inShape, dtype inA = 1, dtype inB = 1)
379  {
380  return detail::extremeValue(generator_, inShape, inA, inB);
381  }
382 
383  //============================================================================
384  // Method Description:
393  template<typename dtype>
394  dtype f(dtype inDofN, dtype inDofD)
395  {
396  return detail::f(generator_, inDofN, inDofD);
397  }
398 
399  //============================================================================
400  // Method Description:
411  template<typename dtype>
412  NdArray<dtype> f(const Shape& inShape, dtype inDofN, dtype inDofD)
413  {
414  return detail::f(generator_, inShape, inDofN, inDofD);
415  }
416 
417  //============================================================================
418  // Method Description:
428  template<typename dtype>
429  dtype gamma(dtype inGammaShape, dtype inScaleValue = 1)
430  {
431  return detail::gamma(generator_, inGammaShape, inScaleValue);
432  }
433 
434  //============================================================================
435  // Method Description:
447  template<typename dtype>
448  NdArray<dtype> gamma(const Shape& inShape, dtype inGammaShape, dtype inScaleValue = 1)
449  {
450  return detail::gamma(generator_, inShape, inGammaShape, inScaleValue);
451  }
452 
453  //============================================================================
454  // Method Description:
463  template<typename dtype>
464  dtype geometric(double inP = 0.5)
465  {
466  return detail::geometric<dtype>(generator_, inP);
467  }
468 
469  //============================================================================
470  // Method Description:
481  template<typename dtype>
482  NdArray<dtype> geometric(const Shape& inShape, double inP = 0.5)
483  {
484  return detail::geometric<dtype>(generator_, inShape, inP);
485  }
486 
487 #ifndef NUMCPP_NO_USE_BOOST
488  //============================================================================
489  // Method Description:
500  template<typename dtype>
501  dtype laplace(dtype inLoc = 0, dtype inScale = 1)
502  {
503  return detail::laplace(generator_, inLoc, inScale);
504  }
505 
506  //============================================================================
507  // Method Description:
520  template<typename dtype>
521  NdArray<dtype> laplace(const Shape& inShape, dtype inLoc = 0, dtype inScale = 1)
522  {
523  return detail::laplace(generator_, inShape, inLoc, inScale);
524  }
525 #endif
526 
527  //============================================================================
528  // Method Description:
539  template<typename dtype>
540  dtype lognormal(dtype inMean = 0, dtype inSigma = 1)
541  {
542  return detail::lognormal(generator_, inMean, inSigma);
543  }
544 
545  //============================================================================
546  // Method Description:
559  template<typename dtype>
560  NdArray<dtype> lognormal(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
561  {
562  return detail::lognormal(generator_, inShape, inMean, inSigma);
563  }
564 
565  //============================================================================
566  // Method Description:
576  template<typename dtype>
577  dtype negativeBinomial(dtype inN, double inP = 0.5)
578  {
579  return detail::negativeBinomial(generator_, inN, inP);
580  }
581 
582  //============================================================================
583  // Method Description:
595  template<typename dtype>
596  NdArray<dtype> negativeBinomial(const Shape& inShape, dtype inN, double inP = 0.5)
597  {
598  return detail::negativeBinomial(generator_, inShape, inN, inP);
599  }
600 
601 #ifndef NUMCPP_NO_USE_BOOST
602  //============================================================================
603  // Method Description:
614  template<typename dtype>
615  dtype nonCentralChiSquared(dtype inK = 1, dtype inLambda = 1)
616  {
617  return detail::nonCentralChiSquared(generator_, inK, inLambda);
618  }
619 
620  //============================================================================
621  // Method Description:
634  template<typename dtype>
635  NdArray<dtype> nonCentralChiSquared(const Shape& inShape, dtype inK = 1, dtype inLambda = 1)
636  {
637  return detail::nonCentralChiSquared(generator_, inShape, inK, inLambda);
638  }
639 #endif
640 
641  //============================================================================
642  // Method Description:
653  template<typename dtype>
654  dtype normal(dtype inMean = 0, dtype inSigma = 1)
655  {
656  return detail::normal(generator_, inMean, inSigma);
657  }
658 
659  //============================================================================
660  // Method Description:
673  template<typename dtype>
674  NdArray<dtype> normal(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
675  {
676  return detail::normal(generator_, inShape, inMean, inSigma);
677  }
678 
679  //============================================================================
680  // Method Description:
688  template<typename dtype>
690  {
691  return detail::permutation(generator_, inValue);
692  }
693 
694  //============================================================================
695  // Method Description:
703  template<typename dtype>
705  {
706  return detail::permutation(generator_, inArray);
707  }
708 
709  //============================================================================
710  // Method Description:
719  template<typename dtype>
720  dtype poisson(double inMean = 1)
721  {
722  return detail::poisson<dtype>(generator_, inMean);
723  }
724 
725  //============================================================================
726  // Method Description:
737  template<typename dtype>
738  NdArray<dtype> poisson(const Shape& inShape, double inMean = 1)
739  {
740  return detail::poisson<dtype>(generator_, inShape, inMean);
741  }
742 
743  //============================================================================
744  // Method Description:
752  template<typename dtype>
753  dtype rand()
754  {
755  return detail::rand<dtype>(generator_);
756  }
757 
758  //============================================================================
759  // Method Description:
769  template<typename dtype>
770  NdArray<dtype> rand(const Shape& inShape)
771  {
772  return detail::rand<dtype>(generator_, inShape);
773  }
774 
775  //============================================================================
776  // Method Description:
788  template<typename dtype>
789  dtype randFloat(dtype inLow, dtype inHigh = 0.)
790  {
791  return detail::randFloat(generator_, inLow, inHigh);
792  }
793 
794  //============================================================================
795  // Method Description:
808  template<typename dtype>
809  NdArray<dtype> randFloat(const Shape& inShape, dtype inLow, dtype inHigh = 0.)
810  {
811  return detail::randFloat(generator_, inShape, inLow, inHigh);
812  }
813 
814  //============================================================================
815  // Method Description:
827  template<typename dtype>
828  dtype randInt(dtype inLow, dtype inHigh = 0)
829  {
830  return detail::randInt(generator_, inLow, inHigh);
831  }
832 
833  //============================================================================
834  // Method Description:
847  template<typename dtype>
848  NdArray<dtype> randInt(const Shape& inShape, dtype inLow, dtype inHigh = 0)
849  {
850  return detail::randInt(generator_, inShape, inLow, inHigh);
851  }
852 
853  //============================================================================
854  // Method Description:
862  template<typename dtype>
863  dtype randN()
864  {
865  return detail::randN<dtype>(generator_);
866  }
867 
868  //============================================================================
869  // Method Description:
879  template<typename dtype>
880  NdArray<dtype> randN(const Shape& inShape)
881  {
882  return detail::randN<dtype>(generator_, inShape);
883  }
884 
885  //============================================================================
886  // Method Description:
891  void seed(int value) noexcept
892  {
893  generator_.seed(value);
894  }
895 
896  //============================================================================
897  // Method Description:
902  template<typename dtype>
903  void shuffle(NdArray<dtype>& inArray)
904  {
905  return detail::shuffle(generator_, inArray);
906  }
907 
908  //============================================================================
909  // Method Description:
918  template<typename dtype>
920  {
921  return detail::standardNormal<dtype>(generator_);
922  }
923 
924  //============================================================================
925  // Method Description:
936  template<typename dtype>
938  {
939  return detail::standardNormal<dtype>(generator_, inShape);
940  }
941 
942  //============================================================================
943  // Method Description:
952  template<typename dtype>
953  dtype studentT(dtype inDof)
954  {
955  return detail::studentT(generator_, inDof);
956  }
957 
958  //============================================================================
959  // Method Description:
970  template<typename dtype>
971  NdArray<dtype> studentT(const Shape& inShape, dtype inDof)
972  {
973  return detail::studentT(generator_, inShape, inDof);
974  }
975 
976 #ifndef NUMCPP_NO_USE_BOOST
977  //============================================================================
978  // Method Description:
990  template<typename dtype>
991  dtype triangle(dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
992  {
993  return detail::triangle(generator_, inA, inB, inC);
994  }
995 
996  //============================================================================
997  // Method Description:
1011  template<typename dtype>
1012  NdArray<dtype> triangle(const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
1013  {
1014  return detail::triangle(generator_, inShape, inA, inB, inC);
1015  }
1016 #endif
1017 
1018  //============================================================================
1019  // Method Description:
1032  template<typename dtype>
1033  dtype uniform(dtype inLow, dtype inHigh)
1034  {
1035  return detail::uniform(generator_, inLow, inHigh);
1036  }
1037 
1038  //============================================================================
1039  // Method Description:
1053  template<typename dtype>
1054  NdArray<dtype> uniform(const Shape& inShape, dtype inLow, dtype inHigh)
1055  {
1056  return detail::uniform(generator_, inShape, inLow, inHigh);
1057  }
1058 
1059 #ifndef NUMCPP_NO_USE_BOOST
1060  //============================================================================
1061  // Method Description:
1070  template<typename dtype>
1071  NdArray<dtype> uniformOnSphere(uint32 inNumPoints, uint32 inDims = 2)
1072  {
1073  return detail::uniformOnSphere<dtype>(generator_, inNumPoints, inDims);
1074  }
1075 #endif
1076 
1077  //============================================================================
1078  // Method Description:
1088  template<typename dtype>
1089  dtype weibull(dtype inA = 1, dtype inB = 1)
1090  {
1091  return detail::weibull(generator_, inA, inB);
1092  }
1093 
1094  //============================================================================
1095  // Method Description:
1107  template<typename dtype>
1108  NdArray<dtype> weibull(const Shape& inShape, dtype inA = 1, dtype inB = 1)
1109  {
1110  return detail::weibull(generator_, inShape, inA, inB);
1111  }
1112 
1113  private:
1114  GeneratorType generator_{};
1115  };
1116 } // namespace nc::random
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
Definition: RNG.hpp:72
dtype randFloat(dtype inLow, dtype inHigh=0.)
Definition: RNG.hpp:789
dtype normal(dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:654
dtype gamma(dtype inGammaShape, dtype inScaleValue=1)
Definition: RNG.hpp:429
NdArray< dtype > binomial(const Shape &inShape, dtype inN, double inP=0.5)
Definition: RNG.hpp:186
bool bernoulli(double inP=0.5)
Definition: RNG.hpp:96
NdArray< dtype > standardNormal(const Shape &inShape)
Definition: RNG.hpp:937
dtype triangle(dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: RNG.hpp:991
NdArray< dtype > randN(const Shape &inShape)
Definition: RNG.hpp:880
dtype randInt(dtype inLow, dtype inHigh=0)
Definition: RNG.hpp:828
NdArray< dtype > choice(const NdArray< dtype > &inArray, uint32 inNum, bool replace=true)
Definition: RNG.hpp:280
NdArray< dtype > uniformOnSphere(uint32 inNumPoints, uint32 inDims=2)
Definition: RNG.hpp:1071
dtype choice(const NdArray< dtype > &inArray)
Definition: RNG.hpp:265
dtype beta(dtype inAlpha, dtype inBeta)
Definition: RNG.hpp:129
NdArray< dtype > uniform(const Shape &inShape, dtype inLow, dtype inHigh)
Definition: RNG.hpp:1054
dtype lognormal(dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:540
NdArray< dtype > beta(const Shape &inShape, dtype inAlpha, dtype inBeta)
Definition: RNG.hpp:149
dtype studentT(dtype inDof)
Definition: RNG.hpp:953
dtype negativeBinomial(dtype inN, double inP=0.5)
Definition: RNG.hpp:577
dtype rand()
Definition: RNG.hpp:753
dtype f(dtype inDofN, dtype inDofD)
Definition: RNG.hpp:394
NdArray< dtype > exponential(const Shape &inShape, dtype inScaleValue=1)
Definition: RNG.hpp:348
NdArray< bool > bernoulli(const Shape &inShape, double inP=0.5)
Definition: RNG.hpp:110
dtype binomial(dtype inN, double inP=0.5)
Definition: RNG.hpp:167
dtype randN()
Definition: RNG.hpp:863
RNG(int seed)
Definition: RNG.hpp:86
NdArray< dtype > poisson(const Shape &inShape, double inMean=1)
Definition: RNG.hpp:738
NdArray< dtype > lognormal(const Shape &inShape, dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:560
NdArray< dtype > normal(const Shape &inShape, dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:674
NdArray< dtype > geometric(const Shape &inShape, double inP=0.5)
Definition: RNG.hpp:482
NdArray< dtype > negativeBinomial(const Shape &inShape, dtype inN, double inP=0.5)
Definition: RNG.hpp:596
dtype geometric(double inP=0.5)
Definition: RNG.hpp:464
NdArray< dtype > randInt(const Shape &inShape, dtype inLow, dtype inHigh=0)
Definition: RNG.hpp:848
dtype chiSquare(dtype inDof)
Definition: RNG.hpp:234
dtype cauchy(dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:201
dtype exponential(dtype inScaleValue=1)
Definition: RNG.hpp:330
NdArray< dtype > gamma(const Shape &inShape, dtype inGammaShape, dtype inScaleValue=1)
Definition: RNG.hpp:448
dtype uniform(dtype inLow, dtype inHigh)
Definition: RNG.hpp:1033
NdArray< dtype > chiSquare(const Shape &inShape, dtype inDof)
Definition: RNG.hpp:252
NdArray< dtype > cauchy(const Shape &inShape, dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:218
void seed(int value) noexcept
Definition: RNG.hpp:891
dtype discrete(const NdArray< double > &inWeights)
Definition: RNG.hpp:296
dtype laplace(dtype inLoc=0, dtype inScale=1)
Definition: RNG.hpp:501
NdArray< dtype > permutation(dtype inValue)
Definition: RNG.hpp:689
dtype weibull(dtype inA=1, dtype inB=1)
Definition: RNG.hpp:1089
NdArray< dtype > studentT(const Shape &inShape, dtype inDof)
Definition: RNG.hpp:971
NdArray< dtype > triangle(const Shape &inShape, dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: RNG.hpp:1012
NdArray< dtype > extremeValue(const Shape &inShape, dtype inA=1, dtype inB=1)
Definition: RNG.hpp:378
NdArray< dtype > rand(const Shape &inShape)
Definition: RNG.hpp:770
dtype standardNormal()
Definition: RNG.hpp:919
void shuffle(NdArray< dtype > &inArray)
Definition: RNG.hpp:903
NdArray< dtype > discrete(const Shape &inShape, const NdArray< double > &inWeights)
Definition: RNG.hpp:314
NdArray< dtype > nonCentralChiSquared(const Shape &inShape, dtype inK=1, dtype inLambda=1)
Definition: RNG.hpp:635
dtype extremeValue(dtype inA=1, dtype inB=1)
Definition: RNG.hpp:362
dtype poisson(double inMean=1)
Definition: RNG.hpp:720
NdArray< dtype > randFloat(const Shape &inShape, dtype inLow, dtype inHigh=0.)
Definition: RNG.hpp:809
NdArray< dtype > f(const Shape &inShape, dtype inDofN, dtype inDofD)
Definition: RNG.hpp:412
NdArray< dtype > weibull(const Shape &inShape, dtype inA=1, dtype inB=1)
Definition: RNG.hpp:1108
dtype nonCentralChiSquared(dtype inK=1, dtype inLambda=1)
Definition: RNG.hpp:615
NdArray< dtype > laplace(const Shape &inShape, dtype inLoc=0, dtype inScale=1)
Definition: RNG.hpp:521
NdArray< dtype > permutation(const NdArray< dtype > &inArray)
Definition: RNG.hpp:704
dtype choice(GeneratorType &generator, const NdArray< dtype > &inArray)
Definition: choice.hpp:52
NdArray< dtype > permutation(GeneratorType &generator, dtype inValue)
Definition: permutation.hpp:52
dtype beta(GeneratorType &generator, dtype inAlpha, dtype inBeta)
Definition: Random/beta.hpp:61
dtype triangle(GeneratorType &generator, dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: triangle.hpp:63
dtype cauchy(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition: cauchy.hpp:55
dtype binomial(GeneratorType &generator, dtype inN, double inP=0.5)
Definition: binomial.hpp:57
dtype chiSquare(GeneratorType &generator, dtype inDof)
Definition: chiSquare.hpp:56
void shuffle(GeneratorType &generator, NdArray< dtype > &inArray)
Definition: shuffle.hpp:48
bool bernoulli(GeneratorType &generator, double inP=0.5)
Definition: Random/bernoulli.hpp:53
dtype randInt(GeneratorType &generator, dtype inLow, dtype inHigh=0)
Definition: randInt.hpp:61
dtype randFloat(GeneratorType &generator, dtype inLow, dtype inHigh=0.)
Definition: randFloat.hpp:61
dtype weibull(GeneratorType &generator, dtype inA=1, dtype inB=1)
Definition: weibull.hpp:56
dtype nonCentralChiSquared(GeneratorType &generator, dtype inK=1, dtype inLambda=1)
Definition: nonCentralChiSquared.hpp:61
dtype laplace(GeneratorType &generator, dtype inLoc=0, dtype inScale=1)
Definition: Random/laplace.hpp:59
dtype normal(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition: normal.hpp:58
dtype uniform(GeneratorType &generator, dtype inLow, dtype inHigh)
Definition: uniform.hpp:54
dtype extremeValue(GeneratorType &generator, dtype inA=1, dtype inB=1)
Definition: extremeValue.hpp:54
dtype studentT(GeneratorType &generator, dtype inDof)
Definition: studentT.hpp:55
dtype gamma(GeneratorType &generator, dtype inGammaShape, dtype inScaleValue=1)
Definition: Random/gamma.hpp:57
dtype negativeBinomial(GeneratorType &generator, dtype inN, double inP=0.5)
Definition: negativeBinomial.hpp:57
dtype lognormal(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition: lognormal.hpp:58
dtype exponential(GeneratorType &generator, dtype inScaleValue=1)
Definition: exponential.hpp:54
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:56
Definition: Random/bernoulli.hpp:41
NdArray< dtype > replace(const NdArray< dtype > &inArray, dtype oldValue, dtype newValue)
Definition: replace.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40