libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
randi.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 
32 #ifdef DOXYGEN_ENGLISH
33 
34 #endif
35 #ifdef DOXYGEN_RUSSIAN
36 
85 #endif
86 int DSPL_API randi(int* x, int n, int start, int stop, random_t* prnd)
87 {
88  double z[RAND_BUFSIZE];
89  double dx;
90  int i, cnt, err;
91  if(!x)
92  return ERROR_PTR;
93  if(n < 1)
94  return ERROR_SIZE;
95 
96  dx = (double)stop - (double)start;
97  cnt = 0;
98  while(cnt < n)
99  {
100  i = cnt % RAND_BUFSIZE;
101  if(!i)
102  {
103  err = randu(z, RAND_BUFSIZE, prnd);
104  if(err != RES_OK)
105  return err;
106  }
107  x[cnt] = start + (int)round(z[i] * dx);
108  cnt++;
109  }
110  return RES_OK;
111 }
112 
113 
Структура параметров датчиков псевдослучайных чисел.
Definition: dspl.h:349
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
#define ERROR_SIZE
Ошибка при передаче размера массива. Данная ошибка возникает когда помимо указателя на массив входных...
Definition: dspl.h:618
int randu(double *x, int n, random_t *prnd)
Генерация вектора равномерно-распределенных в интервале от 0 до 1 псевдослучайных чисел.
Definition: randu.c:103
int randi(int *x, int n, int start, int stop, random_t *prnd)
Генерация целочисленного вектора равномерно распределенных псевдослучайных чисел.
Definition: randi.c:86
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558