NFFT  3.4.1
rand.c
1 /*
2  * Copyright (c) 2002, 2017 Jens Keiner, Stefan Kunis, Daniel Potts
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "infft.h"
20 
21 R Y(drand48)(void)
22 {
23 #ifdef HAVE_DRAND48
24  return (R)(drand48());
25 #else
26  return ((R)rand())/((R)RAND_MAX);
27 #endif
28 }
29 
30 void Y(srand48)(long int seed)
31 {
32 #ifdef HAVE_SRAND48
33  srand48(seed);
34 #else
35  srand((unsigned int)seed);
36 #endif
37 }
38 
39 void Y(vrand_unit_complex)(C *x, const INT n)
40 {
41  INT k;
42 
43  for (k = 0; k < n; k++)
44  x[k] = Y(drand48)() + II * Y(drand48)();
45 }
46 
47 void Y(vrand_shifted_unit_double)(R *x, const INT n)
48 {
49  INT k;
50 
51  for (k = 0; k < n; k++)
52  x[k] = Y(drand48)() - K(0.5);
53 }
54 
55 void Y(vrand_real)(R *x, const INT n, const R a, const R b)
56 {
57  INT k;
58 
59  for (k = 0; k < n; k++)
60  x[k] = a + Y(drand48)() * (b - a);
61 }