libdspl-2.0 Digital Signal Processing Algorithm Library
Pseudo-random numbers generation

Functions

int DSPL_API random_init (random_t *prnd, int type, void *seed)
Pseudorandom numbers generators initialization. More...

int DSPL_API randb (double *x, int n, random_t *prnd)
Binary unipolar [0, 1] pseudorandom vector. More...

int DSPL_API randb2 (double *x, int n, random_t *prnd)
Binary bipolar [-1, 1] pseudorandom vector. More...

Detailed Description

This group describes the functions of the pseudo-random numbers generation by different algorithms.

◆ randb()

 int randb ( double * x, int n, random_t * prnd )

Binary unipolar [0, 1] pseudorandom vector.

The function generates a unipolar pseudo-random vector, each element of which takes an equally probable value of 0 or 1

Parameters
 [in,out] x Pointer to the unipolar pseudo-random vector. Vector size is [n x 1]. Memory must be allocated. [in] n Size of vector x. [in] prnd Pointer to the random_t structure. The structure must be pre-filled with the random_init function. This pointer can be NULL, then it will be used built-in pseudorandom generator defined by the C language standard. However, this mode is not recommended, for example in cryptography and other tasks. There is no guarantee of the quality of the pseudorandom numbers produced if the prnd parameter is set toNULL.
Returns
RES_OK — if pseudorandom vector is calculated successfully.
Else code error.

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dspl.h"
#define N 100
int main(int argc, char* argv[])
{
void* hdspl; /* DSPL handle */
void* hplot; /* GNUPLOT handles */
random_t rnd = {0}; /* random structure */
double x[N];
int err;
err = random_init(&rnd, RAND_TYPE_MRG32K3A, NULL);
if(err != RES_OK)
goto exit_label;
err = randb(x, N, &rnd);
if(err != RES_OK)
goto exit_label;
writetxt(x, NULL, N, "dat/randb_mrg32k3a.txt");
err = randb2(x, N, &rnd);
if(err != RES_OK)
goto exit_label;
writetxt(x, NULL, N, "dat/randb2_mrg32k3a.txt");
/**************************************************************************/
/* plotting by GNUPLOT */
/**************************************************************************/
/* Create window plot */
err = gnuplot_create(argc, argv, 820, 320, "img/randb_test.png", &hplot);
if(err != RES_OK)
goto exit_label;
gnuplot_cmd(hplot, "set xlabel 'x'");
gnuplot_cmd(hplot, "set ylabel 'y'");
gnuplot_cmd(hplot, "unset key");
gnuplot_cmd(hplot, "set yrange[-1.2:1.2]");
gnuplot_cmd(hplot, "set title 'randb, randb2'");
gnuplot_cmd(hplot, "plot 'dat/randb_mrg32k3a.txt' with lines, \\");
gnuplot_cmd(hplot, " 'dat/randb2_mrg32k3a.txt' with lines");
exit_label:
printf("Error code: %x\n", err);
if(hplot)
gnuplot_close(hplot);
/* free dspl handle */
dspl_free(hdspl);
return 0;
}

Program genrates unipolar [0, 1] and bipolar[-1, 1] pseudorandom binary vectors.

As a result of the program run, you can see the graph:

Definition at line 244 of file randgen.c.

◆ randb2()

 int randb2 ( double * x, int n, random_t * prnd )

Binary bipolar [-1, 1] pseudorandom vector.

The function generates a unipolar pseudo-random vector, each element of which takes an equally probable value of -1 or 1

Parameters
 [in,out] x Pointer to the bipolar pseudorandom vector. Vector size is [n x 1]. Memory must be allocated. [in] n Size of vector x. [in] prnd Pointer to the random_t structure. The structure must be pre-filled with the random_init function. This pointer can be NULL, then it will be used built-in pseudorandom generator defined by the C language standard. However, this mode is not recommended, for example in cryptography and other tasks. There is no guarantee of the quality of the pseudorandom numbers produced if the prnd parameter is set toNULL.
Returns
RES_OK — if pseudorandom vector is calculated successfully.
Else code error.

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dspl.h"
#define N 100
int main(int argc, char* argv[])
{
void* hdspl; /* DSPL handle */
void* hplot; /* GNUPLOT handles */
random_t rnd = {0}; /* random structure */
double x[N];
int err;
err = random_init(&rnd, RAND_TYPE_MRG32K3A, NULL);
if(err != RES_OK)
goto exit_label;
err = randb(x, N, &rnd);
if(err != RES_OK)
goto exit_label;
writetxt(x, NULL, N, "dat/randb_mrg32k3a.txt");
err = randb2(x, N, &rnd);
if(err != RES_OK)
goto exit_label;
writetxt(x, NULL, N, "dat/randb2_mrg32k3a.txt");
/**************************************************************************/
/* plotting by GNUPLOT */
/**************************************************************************/
/* Create window plot */
err = gnuplot_create(argc, argv, 820, 320, "img/randb_test.png", &hplot);
if(err != RES_OK)
goto exit_label;
gnuplot_cmd(hplot, "set xlabel 'x'");
gnuplot_cmd(hplot, "set ylabel 'y'");
gnuplot_cmd(hplot, "unset key");
gnuplot_cmd(hplot, "set yrange[-1.2:1.2]");
gnuplot_cmd(hplot, "set title 'randb, randb2'");
gnuplot_cmd(hplot, "plot 'dat/randb_mrg32k3a.txt' with lines, \\");
gnuplot_cmd(hplot, " 'dat/randb2_mrg32k3a.txt' with lines");
exit_label:
printf("Error code: %x\n", err);
if(hplot)
gnuplot_close(hplot);
/* free dspl handle */
dspl_free(hdspl);
return 0;
}

Program genrates unipolar [0, 1] and bipolar[-1, 1] pseudorandom binary vectors.

As a result of the program run, you can see the graph:

Definition at line 358 of file randgen.c.

◆ random_init()

 int random_init ( random_t * prnd, int type, void * seed )

Pseudorandom numbers generators initialization.

Parameters
 [in,out] prnd Pointer to the pseudorandom generators parameters and state vectors. [in] type Pseudorandom generator algorithm: RAND_TYPE_MRG32K3A - 32-bits MRG32K3A generator RAND_TYPE_MT19937 - 64-bits MT19937-64 generator  [in] seed Pointer to the generator start initialization. Type of this pointer is void* because different generators are using different initial values. For example, if we initialize the MRG32K3A generator, then the type parameter is specified asRAND_TYPE_MRG32K3A, and seed converts to double pointer: random_t rnd = {0}; double seed = 1234.0; random_init(&rnd, RAND_TYPE_MRG32K3A, (void*)&seed); For 64-bits Mersenne Twister pseudorandom number generator (type sets as RAND_TYPE_MT19937), seed converts to the unsigned long long pointer: random_t rnd = {0}; unsigned long long seed = 1234353456; random_init(&rnd, RAND_TYPE_MT19937, (void*)&seed); Pseudorandom numbers will be repeated each program restart if seed value is the same. The seed pointer can be NULL. Pseudorandom generators will be initialized by pseudorandom numbers in this case and program will generate different pseudorandom numbers each restart.

Definition at line 121 of file randgen.c.

Definition: dspl.h:350
void DSPL_API gnuplot_close(void *h)
Close GNUPLOT handle.
Definition: gnuplot.c:325
void DSPL_API gnuplot_cmd(void *h, char *cmd)
Function sends cmd command to GNUPLOT corresponds to h handle.
Definition: gnuplot.c:390
int DSPL_API writetxt(double *x, double *y, int n, char *fn)
Save real data to the text file fn. .
Definition: inout.c:491
int DSPL_API random_init(random_t *prnd, int type, void *seed)
Pseudorandom numbers generators initialization.
Definition: randgen.c:121
int DSPL_API randb(double *x, int n, random_t *prnd)
Binary unipolar [0, 1] pseudorandom vector.
Definition: randgen.c:244
void dspl_free(void *handle)
Cleans up the previously linked DSPL-2.0 dynamic library.
#define RES_OK
The function completed correctly. No errors.
Definition: dspl.h:558
int DSPL_API gnuplot_create(int argc, char *argv[], int w, int h, char *fn_png, void **hplot)
Create GNUPLOT chart.
Definition: gnuplot.c:200
int DSPL_API randb2(double *x, int n, random_t *prnd)
Binary bipolar [-1, 1] pseudorandom vector.
Definition: randgen.c:358