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

KDECore

krandom.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (c) 1999 Matthias Kalle Dalheimer <kalle@kde.org>
00003     Copyright (c) 2000 Charles Samuels <charles@kde.org>
00004     Copyright (c) 2005 Joseph Wenninger <kde@jowenn.at>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020         */
00021 
00022 #include "krandom.h"
00023 
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 #include <stdio.h>
00027 #include <time.h>
00028 #include <sys/time.h>
00029 #include <fcntl.h>
00030 #include <kde_file.h>
00031 
00032 int KRandom::random()
00033 {
00034    static bool init = false;
00035    if (!init)
00036    {
00037       unsigned int seed;
00038       init = true;
00039       int fd = KDE_open("/dev/urandom", O_RDONLY);
00040       if (fd < 0 || ::read(fd, &seed, sizeof(seed)) != sizeof(seed))
00041       {
00042             // No /dev/urandom... try something else.
00043             srand(getpid());
00044             seed = rand()+time(0);
00045       }
00046       if (fd >= 0) close(fd);
00047       srand(seed);
00048    }
00049    return rand();
00050 }
00051 
00052 QString KRandom::randomString(int length)
00053 {
00054    if (length <=0 ) return QString();
00055 
00056    QString str; str.resize( length );
00057    int i = 0;
00058    while (length--)
00059    {
00060       int r=random() % 62;
00061       r+=48;
00062       if (r>57) r+=7;
00063       if (r>90) r+=6;
00064       str[i++] =  char(r);
00065       // so what if I work backwards?
00066    }
00067    return str;
00068 }

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal