• https://me.yahoo.com
COST (GBP)
4.99
5.10
0

# Random Sample

Generates random numbers following a Chi-Square distribution.
Controller: CodeCogs

C++
Excel

## Class RandomSample

The chi-square distribution is a univariate distribution which results when univariate-normal variables are squared, and possibly summed. While the normal distribution is symmetric, the chi-square distribution is skewed to the right, and has a minimum of 0. The degrees of freedom of the resulting chi-square distribution are equal to the number of variables that are summed. The mean of a chi-square distribution is equal to its degrees of freedom, and the variance is equal to twice the degrees of freedom.

These chi-square distributions are themselves additive. That is, a chi-square-distributed variable with $\inline&space;&space;d_1$ degrees of freedom can be added to one with $\inline&space;&space;d_2$ degrees of freedom to yield a chi-square-distributed variable with $\inline&space;&space;d_1&space;+&space;d_2$ degrees of freedom, as long as the two added variables are independent. Analogous results can be obtained by subtraction, under the same condition.

The probability density function for the chi-square distribution with $\inline&space;&space;r$ degrees of freedom is given by
$P_r(x)&space;=&space;\frac{&space;x&space;^&space;{r&space;/&space;2&space;-&space;1}&space;\mathrm{e}&space;^&space;{-&space;x&space;/&space;2}&space;}&space;{&space;\Gamma(\frac{1}{2}&space;r)&space;2&space;^&space;{r&space;/&space;2}&space;}$

If some or all of the original normal variables had nonzero mean, then the result will be a noncentral chi-square distribution, with noncentrality parameter $\inline&space;&space;Q$. The effect of the noncentrality parameter is to move the distribution to the right and to make it appear flatter and more symmetrical. Adding two independent noncentral chi-square distributed variables with noncentrality parameters $\inline&space;&space;Q_1$ and $\inline&space;&space;Q_2$yields a noncentral chi-square distributed variable with noncentrality parameter $\inline&space;&space;Q_1&space;+&space;Q_2$.

Using this class, the diagram below is generated from two distinct sequences of 1000 random numbers. Each pair of numbers are plotted against each other, to illustrate the Chi-Square behaviour of this non-uniform random number generator.

## Speed:

The average running time for generating 100,000,000 random numbers using this class on a 750MHz microprocessor is 76 seconds.

## References:

• Ed Rigdon's structural equation modeling FAQ, http://www2.gsu.edu/~mkteer/semfaq.html
• MathWorld, http://mathworld.wolfram.com/Chi-SquaredDistribution.html
• The Newran03 random number generator library of Robert Davies, http://www.robertnz.net/nr03doc.htm

### Example 1

The following example displays 40 random floating point numbers from a chi-square distribution. It uses two different generators to achieve this. The first generator uses a particular value to initialize the seed, while the second one is using the system timer. Notice that it was necessary to divide the timer with the MERSENNEDIV value in order to keep the seed in the (0, 1) interval. Since the seed of the first generator is never changed, the first 20 numbers will always remain the same. However since the second generator is initialized via the system timer, the next 20 numbers will obviously vary with each execution of the program,
#include <iostream>
#include <time.h>
#include <codecogs/stats/dists/continuous/chisquared/randomsample.h>
using namespace std;

int main()
{
Stats::Dists::Continuous::ChiSquared::RandomSample A(10, 12.57, 0.813);
Stats::Dists::Continuous::ChiSquared::RandomSample B(13, 11.06, time(0) / MERSENNEDIV);

for (int i = 0; i < 20; ++i)
cout << A.genReal() << endl;
cout << endl;

for (int i = 0; i < 20; ++i)
cout << B.genReal() << endl;
return 0;
}
Below you will find 20 numbers corresponding to the output of the first generator :
13.1348
34.8509
29.1836
21.0783
20.1372
20.8733
16.2834
26.6546
26.4716
30.2577
38.5005
21.1584
23.6544
24.2537
25.5915
14.5727
14.215
16.0628
25.3528
18.4918

### Authors

Lucian Bentea
##### Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.

## Unknown

 RandomSample::RandomSampleunknown( int df double noncen double s )
Constructor that sets up the class variables and initializes the associated random number generator with the given seed.

### Parameters

 s random number seed
##### Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.

## Sample

 doublesample( int df double noncen double seed = 0 )
This function is a simple wrapper around the randomsample class provided in this module. It uses a static to keep a single instance of this class, so that each call to this function returns a new random number. As a result this function is not necessarily thread safe, in the sense that with identical initial seed, the sequence of random numbers may differ on a multitasking or multi threaded system.

The seed is only set on the first call to this function. Thereafter this parameter is ignored. If you do no want to set the seed, then we suggest you use the system clock the first time you call this function, i.e.
#include <time.h>
...
sample(df, noncen, time(0) / MERSENNEDIV);
If you require more advance behaviour, we strongly recommend that directly use the underlying class randomsample that is provided with this module.

There is an error with your graph parameters for sample with options df=2 noncen=3 seed=0.1:0.9

Error Message:Function sample failed. Ensure that: Invalid C++

### Parameters

 df degree of freedom seed sets the initial seed for the random generator. Only used in the first call to this function
##### Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.