Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Random values and probability distribution
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//random number generators and distributions #include <iostream> #include <random> using namespace std; void UniformIntDist(){ const int nRolls = 100000; // number of experiments const int nStars = 95; // maximum number of stars to distribute default_random_engine generator; uniform_int_distribution<int>distribution(0, 9); int p[10] = {}; for(int i=0; i<nRolls; ++i){ int number = distribution(generator); ++p[number]; } cout << "uniform_int_distribution<int>distribution(0, 9):\n\n"; for(int i=0; i<10; ++i){ cout << i << ":" << string(p[i]*nStars/nRolls, '*') << endl; } }; void NormDist(){ const int nRolls = 100000; // number of experiments const int nStars = 100; // max numebr of stars const int classInt = 10; // class interval default_random_engine rvGenerator; normal_distribution<double>NDist(5.0, 2.0); int p[10] = {}; for(int i = 0; i < nRolls; i++){ double number = NDist(rvGenerator); if((number >= 0) && (number < 10)) ++p[int(number)]; } cout << "Normal Distribution: NDist(5.0, 2.0)\n\n"; for(int i=0; i<classInt; i++){ cout << i << " - " << (i+1) << "\t"; cout << string(p[i]*nStars/nRolls, '*') << endl; } }; void UniformRealDist(){ const int nRolls = 100000; // number of experiments const int nStars = 95; // max number of stars const int nIntervals = 10; // number of intervals default_random_engine generator; uniform_real_distribution<double>distribution(0.0, 1.0); int p[nIntervals] = {0}; for(int i=0; i<nRolls; ++i){ double number = distribution(generator); ++p[int(number*nIntervals)]; } cout << "uniform_real_distribution<double>distribution(0.0, 1.0)" << endl; cout.setf(ios::fixed); cout.precision(1); cout << endl; for(int i=0; i<nIntervals; ++i){ cout << float(i)/nIntervals << " - " << float(i+1)/nIntervals << " : "; cout << string(p[i]*nStars/nRolls, '*') << endl; } }; void inspect(){ default_random_engine gen; uniform_real_distribution<double>d1(0.0, 100.0); uniform_real_distribution<double>d2(d1.param()); cout << "Two independent random values:\n\n"; cout << "Random value 1 = " << d1(gen) << endl; cout << "Random value 2 = " << d2(gen) << endl; }; int main() { //UniformIntDist(); //cout << endl; //UniformRealDist(); // cout << endl; //inspect(); // cout << endl; NormDist(); return 0; }
clang++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 1.1 sec, absolute running time: 0.14 sec, cpu time: 0.01 sec, memory peak: 3 Mb, absolute service time: 1.25 sec
edit mode
|
history
|
discussion