NEURON
scoprand.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 #include <stdlib.h>
3 #ifdef HAVE_UNISTD_H
4 #include <unistd.h>
5 #endif
6 
7 /* this was removed from the scopmath library since there could be
8 multiple copies of the static value below. One in neuron.exe and the
9 other in nrnmech.dll.
10 */
11 
12 /******************************************************************************
13  *
14  * File: random.cpp
15  *
16  * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990
17  * Duke University
18  *
19  ******************************************************************************/
20 
21 #include <math.h>
22 #include "oc_mcran4.hpp"
23 #include "mcran4.h"
24 #include "scoplib.h"
25 static uint32_t value = 1;
26 
27 /*-----------------------------------------------------------------------------
28  *
29  * SCOP_RANDOM()
30  *
31  * Selects a random number from the uniform distribution on
32  * the interval [0,1]. A seed number can be specified by a
33  * call to the function set_seed(seed). Otherwise, a seed
34  * of 1 will be used.
35  *
36  * Calling sequence:
37  * scop_random()
38  *
39  * Arguments:
40  * none for random; for set_seed
41  * Input: seed, int value of the seed
42  *
43  * Output: argument unchanged
44  *
45  *
46  * Returns:
47  * Double precision value of the random number
48  *
49  * Functions called:
50  * none
51  *
52  * Files accessed:
53  * none
54  *
55  *--------------------------------------------------------------------------- */
56 
57 double scop_random(void) {
58  if (use_mcran4()) {
59  /*perhaps 4 times slower but much higher quality*/
60  return mcell_ran4a(&value);
61  } else {
62  uint32_t a = 2147437301, c = 453816981,
63  /* m = 2^32 - 1, the largest long int value that can be represented */
64  /*m = 0xFFFFFFFF;*/ /* limited to 32 bit integers*/
65  m = ~0;
66  value = a * value + c;
67  return (fabs((double) value / (double) m));
68  }
69 }
70 
71 /*-----------------------------------------------------------------------------
72  *
73  * SET_SEED()
74  *
75  * Set random number seed
76  *
77  * Calling sequence:
78  * set_seed(seed)
79  *
80  * Arguments:
81  * seed - integer random number seed
82  *
83  * Returns:
84  * nothing
85  *
86  * Functions called:
87  * none
88  *
89  * Files accessed:
90  * none
91  *
92  */
93 
94 void set_seed(double seed) {
95  value = (uint32_t) seed;
96 }
static int c
Definition: hoc.cpp:169
double mcell_ran4a(uint32_t *high)
Definition: mcran4.cpp:67
fabs
Definition: extdef.h:3
bool use_mcran4()
Definition: oc_mcran4.cpp:10
double scop_random(void)
Definition: scoprand.cpp:57
void set_seed(double seed)
Definition: scoprand.cpp:94
static uint32_t value
Definition: scoprand.cpp:25