libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
randn.c
1 /*
2 * Copyright (c) 2015-2024 Sergey Bakhurin
3 * Digital Signal Processing Library [http://dsplib.org]
4 *
5 * This file is part of libdspl-2.0.
6 *
7 * is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * DSPL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #include <stdlib.h>
23 #include <math.h>
24 #include <time.h>
25 
26 #include "dspl.h"
27 #include "randomgen.h"
28 
29 
30 
31 #ifdef DOXYGEN_ENGLISH
32 
33 #endif
34 #ifdef DOXYGEN_RUSSIAN
35 
88 #endif
89 int DSPL_API randn(double* x, int n, double mu, double sigma, random_t* prnd)
90 {
91  int k, m;
92  double x1[RAND_BUFSIZE], x2[RAND_BUFSIZE];
93  int res;
94  if(!x)
95  return ERROR_PTR;
96 
97  if(n<1)
98  return ERROR_SIZE;
99 
100  if(sigma < 0.0)
101  return ERROR_RAND_SIGMA;
102 
103  k=0;
104  while(k < n)
105  {
106  if((res = randu(x1, RAND_BUFSIZE, prnd)) != RES_OK)
107  goto exit_label;
108  if((res = randu(x2, RAND_BUFSIZE, prnd)) != RES_OK)
109  goto exit_label;
110  m = 0;
111  while(k < n && m < RAND_BUFSIZE)
112  {
113  if(x1[m] != 0.0)
114  {
115  x[k] = sqrt(-2.0*log(x1[m]))*cos(M_2PI*x2[m])*sigma + mu;
116  k++;
117  m++;
118  }
119  }
120  }
121 
122  res = RES_OK;
123 exit_label:
124  return res;
125 }
126 
Структура параметров датчиков псевдослучайных чисел.
Definition: dspl.h:349
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
#define ERROR_SIZE
Ошибка при передаче размера массива. Данная ошибка возникает когда помимо указателя на массив входных...
Definition: dspl.h:618
int randn(double *x, int n, double mu, double sigma, random_t *prnd)
Генерация вектора нормально распределенных псевдослучайных чисел.
Definition: randn.c:89
int randu(double *x, int n, random_t *prnd)
Генерация вектора равномерно-распределенных в интервале от 0 до 1 псевдослучайных чисел.
Definition: randu.c:103
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558
#define ERROR_RAND_SIGMA
Неверно задано среднеквадратическое отклонение нормального распределения случайной величины....
Definition: dspl.h:613