26 #ifndef XMIPPCORE_CORE_XMIPP_MARSAGLIA_H_ 27 #define XMIPPCORE_CORE_XMIPP_MARSAGLIA_H_ 63 long pointer_in_memory;
66 long Number_of_Numbers;
81 Init(fn_in, No_Numbers);
94 pointer_in_memory = 0;
95 Number_of_Numbers = No_Numbers;
96 Type_size =
sizeof(T);
98 std::ifstream
in(fn_in.c_str());
99 in.seekg(0, std::ios::end);
100 std::streampos sp =
in.tellg();
101 if (sp < Number_of_Numbers * Type_size)
109 random_vector =
new char[(Number_of_Numbers * Type_size)];
110 T_random_vector = (T*) random_vector;
112 (std::streamoff)(Number_of_Numbers * Type_size)))), std::ios::beg);
113 in.read(random_vector, (Number_of_Numbers * Type_size));
117 if (
typeid(
double) ==
typeid(T))
128 if (pointer_in_memory >= Number_of_Numbers)
130 (Number_of_Numbers - 1)));
131 return (T_random_vector[pointer_in_memory++]);
138 if (
typeid(
double) !=
typeid(T) &&
typeid(
double) !=
typeid(T))
140 "Marsaglia: I do not know how to calculate integer logs");
142 for (
int hh = 0; hh < Number_of_Numbers; hh++)
143 if (T_random_vector[hh] == 0.)
144 T_random_vector[hh] = -1e+20
f;
146 T_random_vector[hh] =
log(T_random_vector[hh]);
153 for (
int hh = 0; hh < Number_of_Numbers; hh++)
154 T_random_vector[hh] *= mul_cte;
161 for (
int hh = 0; hh < Number_of_Numbers; hh++)
162 T_random_vector[hh] &= mod_cte;
169 for (
int hh = 0; hh < Number_of_Numbers; hh++)
170 T_random_vector[hh] += add_cte;
178 Type_size =
sizeof(T);
180 std::ifstream
in(fn_in.c_str());
181 in.seekg(0, std::ios::end);
182 std::streampos sp =
in.tellg();
184 if (power_of_2 == m_max)
186 T mask = power_of_2 - 1;
191 (std::streamoff)(Number_of_Numbers*Type_size)))), std::ios::beg);
192 for (
int ii = 0; ii < Number_of_Numbers;)
194 aux_number = T_random_vector[ii];
196 if (aux_number > m_max ||
197 (T_random_vector[ii] <= 0) && (aux_number == 0))
201 (sp - (std::streamoff)(Number_of_Numbers*Type_size)))),
203 in.read((
char*) &(T_random_vector[ii]), Type_size);
207 T_random_vector[ii] = aux_number * (T)
SGN(T_random_vector[ii]);
226 unsigned int* int_random_vector;
227 long long MaxInteger;
228 static_assert(
sizeof(
double) !=
sizeof(
int),
"I do not know how to make the double correction");
229 MaxInteger = (
long long) pow(2.0,
sizeof(
unsigned int) * 8.0);
230 int_random_vector = (
unsigned int*) random_vector;
231 for (
int hh = 0; hh < Number_of_Numbers; hh++)
232 T_random_vector[hh] = (T)((double) int_random_vector[hh] /
233 (
double) MaxInteger);
#define REPORT_ERROR(nerr, ErrormMsg)
void M_max(const FileName &fn_in, T m_max)
Marsaglia(const FileName &fn_in, int No_Numbers)
void Init(const FileName &fn_in, int No_Numbers)
void log(Image< double > &op)
Marsaglia()
Empty constructor.
void operator &=(T mod_cte)
#define NEXT_POWER_OF_2(x)
unsigned int randomize_random_generator()