NAG C Library Function Document

nag_rand_dirichlet (g05sec)


nag_rand_dirichlet (g05sec) generates a vector of pseudorandom numbers taken from a Dirichlet distribution.


#include <nag.h>
#include <nagg05.h>
void  nag_rand_dirichlet (Nag_OrderType order, Integer n, Integer m, const double a[], Integer state[], double x[], Integer pdx, NagError *fail)


The distribution has PDF (probability density function)
fx = 1 Bα i=1 m x i αi - 1 and Bα = i=1 m Γ αi Γ i=1 m αi  
where x = x1,x2,,xm  is a vector of dimension m, such that xi>0 for all i and i=1 m xi=1.
nag_rand_dirichlet (g05sec) generates a draw from a Dirichlet distribution by first drawing m independent samples, yigammaαi,1, i.e., independent draws from a gamma distribution with parameters αi>0 and one, and then setting xi=yi/ j=1 m yj.
One of the initialization functions nag_rand_init_repeatable (g05kfc) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeatable (g05kgc) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_dirichlet (g05sec).


Dagpunar J (1988) Principles of Random Variate Generation Oxford University Press
Hastings N A J and Peacock J B (1975) Statistical Distributions Butterworth


1:     order Nag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by order=Nag_RowMajor. See Section in How to Use the NAG Library and its Documentation for a more detailed explanation of the use of this argument.
Constraint: order=Nag_RowMajor or Nag_ColMajor.
2:     n IntegerInput
On entry: n, the number of pseudorandom numbers to be generated.
Constraint: n0.
3:     m IntegerInput
On entry: m, the number of dimensions of the distribution.
Constraint: m>0.
4:     a[m] const doubleInput
On entry: the parameter vector for the distribution.
Constraint: a[i-1]>0.0, for i=1,2,,m.
5:     state[dim] IntegerCommunication Array
Note: the dimension, dim, of this array is dictated by the requirements of associated functions that must have been previously called. This array MUST be the same array passed as argument state in the previous call to nag_rand_init_repeatable (g05kfc) or nag_rand_init_nonrepeatable (g05kgc).
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
6:     x[dim] doubleOutput
Note: the dimension, dim, of the array x must be at least
  • max1,pdx×m when order=Nag_ColMajor;
  • max1,n×pdx when order=Nag_RowMajor.
Where Xi,j appears in this document, it refers to the array element
  • x[j-1×pdx+i-1] when order=Nag_ColMajor;
  • x[i-1×pdx+j-1] when order=Nag_RowMajor.
On exit: the n pseudorandom numbers from the specified Dirichlet distribution, with Xi,j holding the jth dimension for the ith variate.
7:     pdx IntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array x.
  • if order=Nag_ColMajor, pdxn;
  • if order=Nag_RowMajor, pdxm.
8:     fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

Dynamic memory allocation failed.
See Section in How to Use the NAG Library and its Documentation for further information.
On entry, argument value had an illegal value.
On entry, m=value.
Constraint: m > 0.
On entry, n=value.
Constraint: n0.
On entry, pdx=value and m=value.
Constraint: pdxm.
On entry, pdx=value and n=value.
Constraint: order=Nag_ColMajor or pdxn.
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
On entry, state vector has been corrupted or not initialized.
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
On entry, at least one a[i]0.


Not applicable.

Parallelism and Performance

nag_rand_dirichlet (g05sec) is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the x06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

Further Comments



This example prints a set of five pseudorandom numbers from a Dirichlet distribution with parameters m=4 and α=2.0,2.0,2.0,2.0, generated by a single call to nag_rand_dirichlet (g05sec), after initialization by nag_rand_init_repeatable (g05kfc).

Program Text

Program Text (g05sece.c)

Program Data


Program Results

Program Results (g05sece.r)