libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
random_init.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 
74 #endif
75 #ifdef DOXYGEN_RUSSIAN
76 
119 #endif
120 int DSPL_API random_init(random_t* prnd, int type, void* seed)
121 {
122  srand(time(NULL));
123 
124  if(!prnd)
125  return RES_OK;
126 
127  switch(type)
128  {
129  case RAND_TYPE_MRG32K3A:
130  /* MRG32k3a init */
131  prnd->mrg32k3a_x[0] = prnd->mrg32k3a_x[1] = 1.0;
132  prnd->mrg32k3a_y[0] = prnd->mrg32k3a_y[1] =
133  prnd->mrg32k3a_y[2] = 1.0;
134  if(seed)
135  prnd->mrg32k3a_x[2] = *((double*)seed);
136  else
137  prnd->mrg32k3a_x[2] = (double) rand() * rand();
138  break;
139  case RAND_TYPE_MT19937:
140  if(seed)
141  mt19937_init_genrand64(*((unsigned long long*)seed), prnd);
142  else
143  mt19937_init_genrand64((unsigned long long)rand()*rand(), prnd);
144  break;
145  default:
146  return ERROR_RAND_TYPE;
147  }
148  prnd->type = type;
149 
150  return RES_OK;
151 }
152 
Структура параметров датчиков псевдослучайных чисел.
Definition: dspl.h:349
double mrg32k3a_y[3]
Definition: dspl.h:354
int type
Definition: dspl.h:360
#define ERROR_RAND_TYPE
Неизвестный датчик псевдослучайных чисел. В библиотеке используются следующие датчики:
Definition: dspl.h:614
int random_init(random_t *prnd, int type, void *seed)
Инициализация датчиков псевдослучайных чисел.
Definition: random_init.c:120
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558
double mrg32k3a_x[3]
Definition: dspl.h:353