Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1/*
2 * Copyright 2020 Twilight
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15*/
16#pragma once
17#include <QObject>
18#include <QRandomGenerator>
19
20#include "./common_autoport.h"
21
22class QRandomGenerator;
23class QQmlEngine;
24class QQmlContext;
25
49class COMMON_AUTOPORT Random : public QObject
50{
51 Q_OBJECT
52 Q_PROPERTY(bool flipCoin READ flipCoin STORED false)
53 Q_PROPERTY(bool flipCoinF READ flipCoinF STORED false)
54
55public:
57 static Random* inst();
58
59 // Often the int versions can yield bad randomization, the float versions are
60 // provided in case they yield better results.
61
67 Q_INVOKABLE float range(const float end) const;
68
76 Q_INVOKABLE int rangeInclusive(const int start, const int end) const;
77
84 Q_INVOKABLE int rangeExclusive(const int start, const int end) const;
85
91 Q_INVOKABLE bool chanceFailure(const int percent) const;
97 Q_INVOKABLE bool chanceFailure(const float percent) const;
98
104 Q_INVOKABLE bool chanceSuccess(const int percent) const;
110 Q_INVOKABLE bool chanceSuccess(const float percent) const;
111
113 bool flipCoin() const;
115 bool flipCoinF() const;
116
117public slots:
123 void qmlProtect(const QQmlEngine* const engine) const;
124
125private slots:
127 void qmlRegister() const;
128
129private:
131 Random();
132
133 QRandomGenerator* rnd = QRandomGenerator::global();
134};
bool chanceFailure(const int percent) const
Did a percent chance fail?
Definition random.cpp:62
bool flipCoinF() const
50/50 coin flip via the float path (chanceSuccess(0.5f)).
Definition random.cpp:89
bool chanceSuccess(const int percent) const
Did a percent chance succeed?
Definition random.cpp:73
void qmlProtect(const QQmlEngine *const engine) const
Pin this object to C++ ownership so QML's GC never deletes it.
Definition random.cpp:94
bool flipCoin() const
50/50 coin flip via the integer path (chanceSuccess(50)).
Definition random.cpp:84
int rangeInclusive(const int start, const int end) const
Random integer in the closed interval [start, end].
Definition random.cpp:42
float range(const float end) const
Random float in the half-open interval [0.00, end).
Definition random.cpp:37
static Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition random.cpp:31
int rangeExclusive(const int start, const int end) const
Random integer in the half-open interval [start, end).
Definition random.cpp:53
Shared-library import/export macro for the common library.
#define COMMON_AUTOPORT
Expands to the correct dllexport/dllimport decoration for this library.