random.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2003 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// epsic/src/util/random.h
10
11#ifndef __random_H
12#define __random_H
13
14#include <complex>
15#include <inttypes.h>
16
17// returns the current microsecond
18uint64_t usec_seed ();
19
20// seeds the random number generator with the current microsecond
21void random_init ();
22
23// uniformly distributed on 0,1
24double random_double ();
25
26template <class T, class U>
27void random_value (T& value, U scale)
28{
29 value = (random_double() - 0.5) * 2.0 * scale;
30}
31
32template <class T, class U>
33void random_value (std::complex<T>& value, U scale)
34{
35 T real=0, imag=0;
36
37 random_value (real, scale);
38 random_value (imag, scale);
39
40 value = std::complex<T> (real, imag);
41}
42
43template <class T, class U>
44void random_vector (T& array, U scale)
45{
46 for (unsigned i=0; i<array.size(); i++)
47 random_value (array[i], scale);
48}
49
50template <class T, class U>
51void random_matrix (T& array, U scale)
52{
53 for (unsigned i=0; i<array.size(); i++)
54 random_vector (array[i], scale);
55}
56
57#endif

Generated using doxygen 1.14.0