• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
  • util
krandom.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (c) 1999 Matthias Kalle Dalheimer <kalle@kde.org>
3  Copyright (c) 2000 Charles Samuels <charles@kde.org>
4  Copyright (c) 2005 Joseph Wenninger <kde@jowenn.at>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20  */
21 
22 #include "krandom.h"
23 
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <time.h>
28 #include <sys/time.h>
29 #include <fcntl.h>
30 #include <kde_file.h>
31 
32 int KRandom::random()
33 {
34  static bool init = false;
35  if (!init)
36  {
37  unsigned int seed;
38  init = true;
39  int fd = KDE_open("/dev/urandom", O_RDONLY);
40  if (fd < 0 || ::read(fd, &seed, sizeof(seed)) != sizeof(seed))
41  {
42  // No /dev/urandom... try something else.
43  srand(getpid());
44  seed = rand()+time(0);
45  }
46  if (fd >= 0) close(fd);
47  srand(seed);
48  }
49  return rand();
50 }
51 
52 QString KRandom::randomString(int length)
53 {
54  if (length <=0 ) return QString();
55 
56  QString str; str.resize( length );
57  int i = 0;
58  while (length--)
59  {
60  int r=random() % 62;
61  r+=48;
62  if (r>57) r+=7;
63  if (r>90) r+=6;
64  str[i++] = char(r);
65  // so what if I work backwards?
66  }
67  return str;
68 }
krandom.h
KRandom::random
int random()
Generates a uniform random number.
Definition: krandom.cpp:32
QString::resize
void resize(int size)
QString
KRandom::randomString
QString randomString(int length)
Generates a random string.
Definition: krandom.cpp:52
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal