hydrogen 1.2.3
Random.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
4 *
5 * http://www.hydrogen-music.org
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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 General Public License
18 * along with this program. If not, see https://www.gnu.org/licenses
19 *
20 */
21
22#include <core/Helpers/Random.h>
23
24namespace H2Core {
25
26float Random::getGaussian( float fStandardDeviation ) {
27 // gaussian distribution -- dimss
28 float x1, x2, w;
29 do {
30 x1 = 2.0 * ( ( ( float ) rand() ) / static_cast<float>(RAND_MAX) ) - 1.0;
31 x2 = 2.0 * ( ( ( float ) rand() ) / static_cast<float>(RAND_MAX) ) - 1.0;
32 w = x1 * x1 + x2 * x2;
33 } while ( w >= 1.0 );
34
35 w = sqrtf( ( -2.0 * logf( w ) ) / w );
36 return x1 * w * fStandardDeviation + 0.0; // tunable
37}
38};
static float getGaussian(float fStandardDeviation)
Draws an uncorrelated random value from a Gaussian distribution of mean 0 and fStandardDeviation.
Definition Random.cpp:26