NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
RNG.hpp
Go to the documentation of this file.
1
29#pragma once
30
31#include <random>
32
33#include "NumCpp/Core/Enums.hpp"
43#include "NumCpp/Random/f.hpp"
64
65namespace nc::random
66{
67 //============================================================================
68 // Class Description:
71 template<typename GeneratorType = std::mt19937_64>
72 class RNG
73 {
74 public:
75 //============================================================================
76 // Method Description:
79 RNG() = default;
80
81 //============================================================================
82 // Method Description:
87 explicit RNG(int seed) :
88 generator_(seed){};
89
90 //============================================================================
91 // Method Description:
97 bool bernoulli(double inP = 0.5)
98 {
99 return detail::bernoulli(generator_, inP);
100 }
101
102 //============================================================================
103 // Method Description:
111 NdArray<bool> bernoulli(const Shape& inShape, double inP = 0.5)
112 {
113 return detail::bernoulli(generator_, inShape, inP);
114 }
115
116#ifndef NUMCPP_NO_USE_BOOST
117 //============================================================================
118 // Method Description:
129 template<typename dtype>
130 dtype beta(dtype inAlpha, dtype inBeta)
131 {
132 return detail::beta(generator_, inAlpha, inBeta);
133 }
134
135 //============================================================================
136 // Method Description:
149 template<typename dtype>
150 NdArray<dtype> beta(const Shape& inShape, dtype inAlpha, dtype inBeta)
151 {
152 return detail::beta(generator_, inShape, inAlpha, inBeta);
153 }
154#endif
155
156 //============================================================================
157 // Method Description:
167 template<typename dtype>
168 dtype binomial(dtype inN, double inP = 0.5)
169 {
170 return detail::binomial(generator_, inN, inP);
171 }
172
173 //============================================================================
174 // Method Description:
186 template<typename dtype>
187 NdArray<dtype> binomial(const Shape& inShape, dtype inN, double inP = 0.5)
188 {
189 return detail::binomial(generator_, inShape, inN, inP);
190 }
191
192 //============================================================================
193 // Method Description:
201 template<typename dtype>
202 dtype cauchy(dtype inMean = 0, dtype inSigma = 1)
203 {
204 return detail::cauchy(generator_, inMean, inSigma);
205 }
206
207 //============================================================================
208 // Method Description:
218 template<typename dtype>
219 NdArray<dtype> cauchy(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
220 {
221 return detail::cauchy(generator_, inShape, inMean, inSigma);
222 }
223
224 //============================================================================
225 // Method Description:
234 template<typename dtype>
235 dtype chiSquare(dtype inDof)
236 {
237 return detail::chiSquare(generator_, inDof);
238 }
239
240 //============================================================================
241 // Method Description:
252 template<typename dtype>
253 NdArray<dtype> chiSquare(const Shape& inShape, dtype inDof)
254 {
255 return detail::chiSquare(generator_, inShape, inDof);
256 }
257
258 //============================================================================
259 // Method Description:
265 template<typename dtype>
266 dtype choice(const NdArray<dtype>& inArray)
267 {
268 return detail::choice(generator_, inArray);
269 }
270
271 //============================================================================
272 // Method Description:
280 template<typename dtype>
282 {
283 return detail::choice(generator_, inArray, inNum, replace);
284 }
285
286 //============================================================================
287 // Method Description:
296 template<typename dtype>
297 dtype discrete(const NdArray<double>& inWeights)
298 {
299 return detail::discrete<dtype>(generator_, inWeights);
300 }
301
302 //============================================================================
303 // Method Description:
314 template<typename dtype>
315 NdArray<dtype> discrete(const Shape& inShape, const NdArray<double>& inWeights)
316 {
317 return detail::discrete<dtype>(generator_, inShape, inWeights);
318 }
319
320 //============================================================================
321 // Method Description:
330 template<typename dtype>
331 dtype exponential(dtype inScaleValue = 1)
332 {
333 return detail::exponential(generator_, inScaleValue);
334 }
335
336 //============================================================================
337 // Method Description:
348 template<typename dtype>
349 NdArray<dtype> exponential(const Shape& inShape, dtype inScaleValue = 1)
350 {
351 return detail::exponential(generator_, inShape, inScaleValue);
352 }
353
354 //============================================================================
355 // Method Description:
362 template<typename dtype>
363 dtype extremeValue(dtype inA = 1, dtype inB = 1)
364 {
365 return detail::extremeValue(generator_, inA, inB);
366 }
367
368 //============================================================================
369 // Method Description:
378 template<typename dtype>
379 NdArray<dtype> extremeValue(const Shape& inShape, dtype inA = 1, dtype inB = 1)
380 {
381 return detail::extremeValue(generator_, inShape, inA, inB);
382 }
383
384 //============================================================================
385 // Method Description:
394 template<typename dtype>
395 dtype f(dtype inDofN, dtype inDofD)
396 {
397 return detail::f(generator_, inDofN, inDofD);
398 }
399
400 //============================================================================
401 // Method Description:
412 template<typename dtype>
413 NdArray<dtype> f(const Shape& inShape, dtype inDofN, dtype inDofD)
414 {
415 return detail::f(generator_, inShape, inDofN, inDofD);
416 }
417
418 //============================================================================
419 // Method Description:
429 template<typename dtype>
430 dtype gamma(dtype inGammaShape, dtype inScaleValue = 1)
431 {
432 return detail::gamma(generator_, inGammaShape, inScaleValue);
433 }
434
435 //============================================================================
436 // Method Description:
448 template<typename dtype>
449 NdArray<dtype> gamma(const Shape& inShape, dtype inGammaShape, dtype inScaleValue = 1)
450 {
451 return detail::gamma(generator_, inShape, inGammaShape, inScaleValue);
452 }
453
454 //============================================================================
455 // Method Description:
464 template<typename dtype>
465 dtype geometric(double inP = 0.5)
466 {
467 return detail::geometric<dtype>(generator_, inP);
468 }
469
470 //============================================================================
471 // Method Description:
482 template<typename dtype>
483 NdArray<dtype> geometric(const Shape& inShape, double inP = 0.5)
484 {
485 return detail::geometric<dtype>(generator_, inShape, inP);
486 }
487
488#ifndef NUMCPP_NO_USE_BOOST
489 //============================================================================
490 // Method Description:
501 template<typename dtype>
502 dtype laplace(dtype inLoc = 0, dtype inScale = 1)
503 {
504 return detail::laplace(generator_, inLoc, inScale);
505 }
506
507 //============================================================================
508 // Method Description:
521 template<typename dtype>
522 NdArray<dtype> laplace(const Shape& inShape, dtype inLoc = 0, dtype inScale = 1)
523 {
524 return detail::laplace(generator_, inShape, inLoc, inScale);
525 }
526#endif
527
528 //============================================================================
529 // Method Description:
540 template<typename dtype>
541 dtype lognormal(dtype inMean = 0, dtype inSigma = 1)
542 {
543 return detail::lognormal(generator_, inMean, inSigma);
544 }
545
546 //============================================================================
547 // Method Description:
560 template<typename dtype>
561 NdArray<dtype> lognormal(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
562 {
563 return detail::lognormal(generator_, inShape, inMean, inSigma);
564 }
565
566 //============================================================================
567 // Method Description:
577 template<typename dtype>
578 dtype negativeBinomial(dtype inN, double inP = 0.5)
579 {
580 return detail::negativeBinomial(generator_, inN, inP);
581 }
582
583 //============================================================================
584 // Method Description:
596 template<typename dtype>
597 NdArray<dtype> negativeBinomial(const Shape& inShape, dtype inN, double inP = 0.5)
598 {
599 return detail::negativeBinomial(generator_, inShape, inN, inP);
600 }
601
602#ifndef NUMCPP_NO_USE_BOOST
603 //============================================================================
604 // Method Description:
615 template<typename dtype>
616 dtype nonCentralChiSquared(dtype inK = 1, dtype inLambda = 1)
617 {
618 return detail::nonCentralChiSquared(generator_, inK, inLambda);
619 }
620
621 //============================================================================
622 // Method Description:
635 template<typename dtype>
636 NdArray<dtype> nonCentralChiSquared(const Shape& inShape, dtype inK = 1, dtype inLambda = 1)
637 {
638 return detail::nonCentralChiSquared(generator_, inShape, inK, inLambda);
639 }
640#endif
641
642 //============================================================================
643 // Method Description:
654 template<typename dtype>
655 dtype normal(dtype inMean = 0, dtype inSigma = 1)
656 {
657 return detail::normal(generator_, inMean, inSigma);
658 }
659
660 //============================================================================
661 // Method Description:
674 template<typename dtype>
675 NdArray<dtype> normal(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
676 {
677 return detail::normal(generator_, inShape, inMean, inSigma);
678 }
679
680 //============================================================================
681 // Method Description:
689 template<typename dtype>
691 {
692 return detail::permutation(generator_, inValue);
693 }
694
695 //============================================================================
696 // Method Description:
704 template<typename dtype>
706 {
707 return detail::permutation(generator_, inArray);
708 }
709
710 //============================================================================
711 // Method Description:
720 template<typename dtype>
721 dtype poisson(double inMean = 1)
722 {
723 return detail::poisson<dtype>(generator_, inMean);
724 }
725
726 //============================================================================
727 // Method Description:
738 template<typename dtype>
739 NdArray<dtype> poisson(const Shape& inShape, double inMean = 1)
740 {
741 return detail::poisson<dtype>(generator_, inShape, inMean);
742 }
743
744 //============================================================================
745 // Method Description:
753 template<typename dtype>
754 dtype rand()
755 {
756 return detail::rand<dtype>(generator_);
757 }
758
759 //============================================================================
760 // Method Description:
770 template<typename dtype>
771 NdArray<dtype> rand(const Shape& inShape)
772 {
773 return detail::rand<dtype>(generator_, inShape);
774 }
775
776 //============================================================================
777 // Method Description:
789 template<typename dtype>
790 dtype randFloat(dtype inLow, dtype inHigh = 0.)
791 {
792 return detail::randFloat(generator_, inLow, inHigh);
793 }
794
795 //============================================================================
796 // Method Description:
809 template<typename dtype>
810 NdArray<dtype> randFloat(const Shape& inShape, dtype inLow, dtype inHigh = 0.)
811 {
812 return detail::randFloat(generator_, inShape, inLow, inHigh);
813 }
814
815 //============================================================================
816 // Method Description:
828 template<typename dtype>
829 dtype randInt(dtype inLow, dtype inHigh = 0)
830 {
831 return detail::randInt(generator_, inLow, inHigh);
832 }
833
834 //============================================================================
835 // Method Description:
848 template<typename dtype>
849 NdArray<dtype> randInt(const Shape& inShape, dtype inLow, dtype inHigh = 0)
850 {
851 return detail::randInt(generator_, inShape, inLow, inHigh);
852 }
853
854 //============================================================================
855 // Method Description:
863 template<typename dtype>
864 dtype randN()
865 {
866 return detail::randN<dtype>(generator_);
867 }
868
869 //============================================================================
870 // Method Description:
880 template<typename dtype>
881 NdArray<dtype> randN(const Shape& inShape)
882 {
883 return detail::randN<dtype>(generator_, inShape);
884 }
885
886 //============================================================================
887 // Method Description:
892 void seed(int value) noexcept
893 {
894 generator_.seed(value);
895 }
896
897 //============================================================================
898 // Method Description:
903 template<typename dtype>
904 void shuffle(NdArray<dtype>& inArray)
905 {
906 return detail::shuffle(generator_, inArray);
907 }
908
909 //============================================================================
910 // Method Description:
919 template<typename dtype>
921 {
922 return detail::standardNormal<dtype>(generator_);
923 }
924
925 //============================================================================
926 // Method Description:
937 template<typename dtype>
939 {
940 return detail::standardNormal<dtype>(generator_, inShape);
941 }
942
943 //============================================================================
944 // Method Description:
953 template<typename dtype>
954 dtype studentT(dtype inDof)
955 {
956 return detail::studentT(generator_, inDof);
957 }
958
959 //============================================================================
960 // Method Description:
971 template<typename dtype>
972 NdArray<dtype> studentT(const Shape& inShape, dtype inDof)
973 {
974 return detail::studentT(generator_, inShape, inDof);
975 }
976
977#ifndef NUMCPP_NO_USE_BOOST
978 //============================================================================
979 // Method Description:
991 template<typename dtype>
992 dtype triangle(dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
993 {
994 return detail::triangle(generator_, inA, inB, inC);
995 }
996
997 //============================================================================
998 // Method Description:
1012 template<typename dtype>
1013 NdArray<dtype> triangle(const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
1014 {
1015 return detail::triangle(generator_, inShape, inA, inB, inC);
1016 }
1017#endif
1018
1019 //============================================================================
1020 // Method Description:
1033 template<typename dtype>
1034 dtype uniform(dtype inLow, dtype inHigh)
1035 {
1036 return detail::uniform(generator_, inLow, inHigh);
1037 }
1038
1039 //============================================================================
1040 // Method Description:
1054 template<typename dtype>
1055 NdArray<dtype> uniform(const Shape& inShape, dtype inLow, dtype inHigh)
1056 {
1057 return detail::uniform(generator_, inShape, inLow, inHigh);
1058 }
1059
1060#ifndef NUMCPP_NO_USE_BOOST
1061 //============================================================================
1062 // Method Description:
1071 template<typename dtype>
1073 {
1074 return detail::uniformOnSphere<dtype>(generator_, inNumPoints, inDims);
1075 }
1076#endif
1077
1078 //============================================================================
1079 // Method Description:
1089 template<typename dtype>
1090 dtype weibull(dtype inA = 1, dtype inB = 1)
1091 {
1092 return detail::weibull(generator_, inA, inB);
1093 }
1094
1095 //============================================================================
1096 // Method Description:
1108 template<typename dtype>
1109 NdArray<dtype> weibull(const Shape& inShape, dtype inA = 1, dtype inB = 1)
1110 {
1111 return detail::weibull(generator_, inShape, inA, inB);
1112 }
1113
1114 private:
1115 GeneratorType generator_{};
1116 };
1117} // namespace nc::random
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
Definition: RNG.hpp:73
dtype randFloat(dtype inLow, dtype inHigh=0.)
Definition: RNG.hpp:790
dtype normal(dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:655
NdArray< dtype > discrete(const Shape &inShape, const NdArray< double > &inWeights)
Definition: RNG.hpp:315
dtype gamma(dtype inGammaShape, dtype inScaleValue=1)
Definition: RNG.hpp:430
bool bernoulli(double inP=0.5)
Definition: RNG.hpp:97
NdArray< dtype > triangle(const Shape &inShape, dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: RNG.hpp:1013
dtype triangle(dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: RNG.hpp:992
dtype randInt(dtype inLow, dtype inHigh=0)
Definition: RNG.hpp:829
NdArray< dtype > cauchy(const Shape &inShape, dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:219
NdArray< dtype > binomial(const Shape &inShape, dtype inN, double inP=0.5)
Definition: RNG.hpp:187
NdArray< dtype > lognormal(const Shape &inShape, dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:561
NdArray< dtype > chiSquare(const Shape &inShape, dtype inDof)
Definition: RNG.hpp:253
dtype choice(const NdArray< dtype > &inArray)
Definition: RNG.hpp:266
NdArray< dtype > randInt(const Shape &inShape, dtype inLow, dtype inHigh=0)
Definition: RNG.hpp:849
dtype beta(dtype inAlpha, dtype inBeta)
Definition: RNG.hpp:130
dtype lognormal(dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:541
NdArray< dtype > standardNormal(const Shape &inShape)
Definition: RNG.hpp:938
dtype studentT(dtype inDof)
Definition: RNG.hpp:954
dtype negativeBinomial(dtype inN, double inP=0.5)
Definition: RNG.hpp:578
dtype rand()
Definition: RNG.hpp:754
NdArray< dtype > permutation(const NdArray< dtype > &inArray)
Definition: RNG.hpp:705
NdArray< dtype > rand(const Shape &inShape)
Definition: RNG.hpp:771
NdArray< dtype > randFloat(const Shape &inShape, dtype inLow, dtype inHigh=0.)
Definition: RNG.hpp:810
dtype f(dtype inDofN, dtype inDofD)
Definition: RNG.hpp:395
dtype binomial(dtype inN, double inP=0.5)
Definition: RNG.hpp:168
NdArray< dtype > beta(const Shape &inShape, dtype inAlpha, dtype inBeta)
Definition: RNG.hpp:150
dtype randN()
Definition: RNG.hpp:864
RNG(int seed)
Definition: RNG.hpp:87
NdArray< dtype > uniform(const Shape &inShape, dtype inLow, dtype inHigh)
Definition: RNG.hpp:1055
NdArray< dtype > nonCentralChiSquared(const Shape &inShape, dtype inK=1, dtype inLambda=1)
Definition: RNG.hpp:636
dtype geometric(double inP=0.5)
Definition: RNG.hpp:465
dtype chiSquare(dtype inDof)
Definition: RNG.hpp:235
dtype cauchy(dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:202
dtype exponential(dtype inScaleValue=1)
Definition: RNG.hpp:331
NdArray< dtype > extremeValue(const Shape &inShape, dtype inA=1, dtype inB=1)
Definition: RNG.hpp:379
NdArray< dtype > randN(const Shape &inShape)
Definition: RNG.hpp:881
dtype uniform(dtype inLow, dtype inHigh)
Definition: RNG.hpp:1034
NdArray< dtype > gamma(const Shape &inShape, dtype inGammaShape, dtype inScaleValue=1)
Definition: RNG.hpp:449
NdArray< dtype > choice(const NdArray< dtype > &inArray, uint32 inNum, Replace replace=Replace::YES)
Definition: RNG.hpp:281
NdArray< dtype > studentT(const Shape &inShape, dtype inDof)
Definition: RNG.hpp:972
NdArray< dtype > permutation(dtype inValue)
Definition: RNG.hpp:690
NdArray< dtype > normal(const Shape &inShape, dtype inMean=0, dtype inSigma=1)
Definition: RNG.hpp:675
NdArray< dtype > negativeBinomial(const Shape &inShape, dtype inN, double inP=0.5)
Definition: RNG.hpp:597
void seed(int value) noexcept
Definition: RNG.hpp:892
dtype discrete(const NdArray< double > &inWeights)
Definition: RNG.hpp:297
dtype laplace(dtype inLoc=0, dtype inScale=1)
Definition: RNG.hpp:502
NdArray< dtype > laplace(const Shape &inShape, dtype inLoc=0, dtype inScale=1)
Definition: RNG.hpp:522
dtype weibull(dtype inA=1, dtype inB=1)
Definition: RNG.hpp:1090
NdArray< dtype > geometric(const Shape &inShape, double inP=0.5)
Definition: RNG.hpp:483
NdArray< bool > bernoulli(const Shape &inShape, double inP=0.5)
Definition: RNG.hpp:111
NdArray< dtype > weibull(const Shape &inShape, dtype inA=1, dtype inB=1)
Definition: RNG.hpp:1109
dtype standardNormal()
Definition: RNG.hpp:920
void shuffle(NdArray< dtype > &inArray)
Definition: RNG.hpp:904
NdArray< dtype > uniformOnSphere(uint32 inNumPoints, uint32 inDims=2)
Definition: RNG.hpp:1072
NdArray< dtype > exponential(const Shape &inShape, dtype inScaleValue=1)
Definition: RNG.hpp:349
dtype extremeValue(dtype inA=1, dtype inB=1)
Definition: RNG.hpp:363
dtype poisson(double inMean=1)
Definition: RNG.hpp:721
NdArray< dtype > f(const Shape &inShape, dtype inDofN, dtype inDofD)
Definition: RNG.hpp:413
NdArray< dtype > poisson(const Shape &inShape, double inMean=1)
Definition: RNG.hpp:739
dtype nonCentralChiSquared(dtype inK=1, dtype inLambda=1)
Definition: RNG.hpp:616
dtype choice(GeneratorType &generator, const NdArray< dtype > &inArray)
Definition: choice.hpp:53
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
Replace
Replace boolean.
Definition: Enums.hpp:101
NdArray< dtype > replace(const NdArray< dtype > &inArray, dtype oldValue, dtype newValue)
Definition: replace.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40