LMMS
Loading...
Searching...
No Matches
Random.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
7
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
10
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
14
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
22
23 ==============================================================================
24*/
25
26#include "Random.h"
27#include "../misc/Time.h"
28
29#include <limits>
30
31namespace water {
32
33Random::Random (const int64 seedValue) noexcept : seed (seedValue)
34{
35}
36
38{
40}
41
45
46void Random::setSeed (const int64 newSeed) noexcept
47{
48 seed = newSeed;
49}
50
51void Random::combineSeed (const int64 seedValue) noexcept
52{
53 seed ^= nextInt64() ^ seedValue;
54}
55
57{
58 static int64 globalSeed = 0;
59
60 combineSeed (globalSeed ^ (int64) (pointer_sized_int) this);
63 globalSeed ^= seed;
64}
65
67{
68 static Random sysRand;
69 return sysRand;
70}
71
72//==============================================================================
74{
75 seed = (seed * 0x5deece66dLL + 11) & 0xffffffffffffLL;
76
77 return (int) (seed >> 16);
78}
79
80int Random::nextInt (const int maxValue) noexcept
81{
82 wassert (maxValue > 0);
83 return (int) ((((unsigned int) nextInt()) * (uint64) maxValue) >> 32);
84}
85
87{
88 return (((int64) nextInt()) << 32) | (int64) (uint64) (uint32) nextInt();
89}
90
92{
93 return (nextInt() & 0x40000000) != 0;
94}
95
97{
98 return static_cast<uint32> (nextInt()) / (static_cast<float>(std::numeric_limits<uint32>::max()) + 1.5f);
99}
100
102{
103 return static_cast<uint32> (nextInt()) / (static_cast<double>(std::numeric_limits<uint32>::max() + 1.5));
104}
105
106}
#define noexcept
Definition DistrhoDefines.h:72
void setSeedRandomly()
Definition Random.cpp:56
Random()
Definition Random.cpp:37
Random(int64 seedValue) noexcept
Definition Random.cpp:33
static Random & getSystemRandom() noexcept
Definition Random.cpp:66
double nextDouble() noexcept
Definition Random.cpp:101
void combineSeed(int64 seedValue) noexcept
Definition Random.cpp:51
int64 seed
Definition Random.h:121
float nextFloat() noexcept
Definition Random.cpp:96
int nextInt() noexcept
Definition Random.cpp:73
int64 nextInt64() noexcept
Definition Random.cpp:86
bool nextBool() noexcept
Definition Random.cpp:91
~Random() noexcept
Definition Random.cpp:42
void setSeed(int64 newSeed) noexcept
Definition Random.cpp:46
#define wassert(expression)
int64 currentTimeMillis() noexcept
Definition Time.cpp:102
uint32 getMillisecondCounter() noexcept
Definition Time.cpp:117
Definition AudioSampleBuffer.h:33
unsigned int uint32
Definition water.h:98
unsigned long long uint64
Definition water.h:102
int pointer_sized_int
Definition water.h:111
long long int64
Definition water.h:100
typedef int(UZ_EXP MsgFn)()