KCoreAddons

krandom.cpp
1/*
2 This file is part of the KDE libraries
3
4 SPDX-FileCopyrightText: 1999 Matthias Kalle Dalheimer <kalle@kde.org>
5 SPDX-FileCopyrightText: 2000 Charles Samuels <charles@kde.org>
6 SPDX-FileCopyrightText: 2005 Joseph Wenninger <kde@jowenn.at>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#include "krandom.h"
12
13#include <stdlib.h>
14#ifdef Q_OS_WIN
15#include <process.h>
16#else // Q_OS_WIN
17#include <unistd.h>
18#endif // Q_OS_WIN
19#include <stdio.h>
20#include <time.h>
21#ifndef Q_OS_WIN
22#include <sys/time.h>
23#endif // Q_OS_WIN
24#include <fcntl.h>
25
26#include <QFile>
27#include <QThread>
28#include <QThreadStorage>
29
31{
32 if (length <= 0) {
33 return QString();
34 }
35
36 QString str;
37 str.resize(length);
38 int i = 0;
39 while (length--) {
40 int r = QRandomGenerator::global()->bounded(62);
41 r += 48;
42 if (r > 57) {
43 r += 7;
44 }
45 if (r > 90) {
46 r += 6;
47 }
48 str[i++] = QLatin1Char(char(r));
49 // so what if I work backwards?
50 }
51 return str;
52}
KCOREADDONS_EXPORT QString randomString(int length)
Generates a random string.
Definition krandom.cpp:30
double bounded(double highest)
QRandomGenerator * global()
void resize(qsizetype newSize, QChar fillChar)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.