KCoreAddons

krandom.h
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 1999 Matthias Kalle Dalheimer <[email protected]>
5  SPDX-FileCopyrightText: 2000 Charles Samuels <[email protected]>
6  SPDX-FileCopyrightText: 2005 Joseph Wenninger <[email protected]>
7 
8  SPDX-License-Identifier: LGPL-2.0-or-later
9 */
10 
11 #ifndef KRANDOM_H
12 #define KRANDOM_H
13 
14 #include <kcoreaddons_export.h>
15 
16 #include <QRandomGenerator>
17 #include <QString>
18 
19 #include <limits>
20 
21 /**
22  * \headerfile krandom.h <KRandom>
23  *
24  * @short Helper class to create random data
25  *
26  * This namespace provides methods which generate random data.
27  * KRandom is not recommended for serious random-number generation needs,
28  * like cryptography.
29  */
30 namespace KRandom
31 {
32 #if KCOREADDONS_ENABLE_DEPRECATED_SINCE(5, 72)
33 /**
34  * Generates a uniform random number.
35  * @return A random number in the range [0, RAND_MAX). The RNG is seeded
36  * on first use.
37  * @deprecated Since 5.72, use QRandomGenerator::global(). The 1:1 port is bounded(RAND_MAX) but check all the methods that QRandomGenerator provides.
38  */
39 KCOREADDONS_DEPRECATED_VERSION(
40  5,
41  72,
42  "Use QRandomGenerator::global(). The 1:1 port is bounded(RAND_MAX) but check see all the methods that QRandomGenerator provides.")
43 KCOREADDONS_EXPORT int random();
44 #endif
45 
46 /**
47  * Generates a random string. It operates in the range [A-Za-z0-9]
48  * @param length Generate a string of this length.
49  * @return the random string
50  */
51 KCOREADDONS_EXPORT QString randomString(int length);
52 
53 /**
54  * Reorders the elements of the given container randomly using the given random number generator.
55  *
56  * The container needs to implement size() and T &operator[]
57  *
58  * @since 5.73
59  */
60 template<typename T>
61 void shuffle(T &container, QRandomGenerator *generator)
62 {
63  Q_ASSERT(container.size() <= std::numeric_limits<int>::max());
64  // Fisher-Yates algorithm
65  for (int index = container.size() - 1; index > 0; --index) {
66  const int swapIndex = generator->bounded(index + 1);
67  qSwap(container[index], container[swapIndex]);
68  }
69 }
70 
71 /**
72  * Reorders the elements of the given container randomly.
73  *
74  * The container needs to implement size() and T &operator[]
75  *
76  * @since 5.73
77  */
78 template<typename T>
79 void shuffle(T &container)
80 {
81  shuffle(container, QRandomGenerator::global());
82 }
83 
84 }
85 
86 #endif
void shuffle(T &container, QRandomGenerator *generator)
Reorders the elements of the given container randomly using the given random number generator...
Definition: krandom.h:61
KCOREADDONS_EXPORT int random()
Generates a uniform random number.
Definition: krandom.cpp:31
Helper class to create random data.
Definition: krandom.h:30
double bounded(double highest)
QRandomGenerator * global()
KCOREADDONS_EXPORT QString randomString(int length)
Generates a random string.
Definition: krandom.cpp:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:01:47 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.