🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: de.comp.lang.c++
Article #1976

Re: Zufallsfunktion unter C++?

#1976
From: Dietmar Kuehl
Date: Wed, 03 Apr 1996 13:46
94 lines
2868 bytes
Hi,

Kurt Watzka (watzka@stat.uni-muenchen.de) wrote:
: erbsloeh@ind.uni-stuttgart.de (Arnold Erbsloeh) writes:

: >Gibt es eine Zufallsfunktion unter C++, aehlich wie in BASIC der Befehl RND?

: Leider, oder besser gluecklicherweise, nicht. Aber die Standart C Bibliothek
: gibt es auch unter C++.

: Je nach Entwicklungsstand des Compilers entweder "#include <cstdlib>" oder
: "#include <stdlib.h>", und dann heisst sie, wer haette es gedacht, rand().

Damit man tatsaechlich Zufallszahlen bekommt, sollt man allerdings
vorher 'srand()' mit einem geeigneten, halbwegs zufaelligen Wert (der
Zeit, der Prozess ID, der Position der Maus, oder was man halt gerade
hat) aufrufen. Hier ist ein Beispiel, wie man es machen koennte (zu beachten ist
allerdings, dass der Startwert nicht unbedingt in jedem Program anders ist: etwa wenn
das Program innerhalb der Genauigkeit von 'time()' zweimal aufgerufen wird...):

  //--- rand.h ---

  class RND
  {
  private:
    struct Init
    {
      Init(int def);
    };
    static Init init;

  public:
    int    max_int() const;
    int    rnd_int() const;
    int operator()() const { return rnd_int(); }
    double max_dbl() const { return 1.0; }
    double rnd_dbl() const { return double(rnd_int()) / double(max_int()); }
  };

  extern RND rnd;

  //--- rand.cc ---

  #include <stdlib.h>
  #include <limits.h>
  #include <time.h>

  #ifndef RAND_MAX
  #  define RAND_MAX INT_MAX
  #endif

  RND::Init::Init(int def)
  {
    typedef unsigned int uint;
    uint   seed = 1;
    time_t t = time(0);

    if (t != -1)
      seed = uint(t);
    else
    {
      clock_t c = clock();

      if (c != -1)
	seed = uint(c);
      // else
      //   do something else e.g. use 'gmtime()'.
    }

    srand(seed);
  }

  RND::Init RND::init = 1;
  RND       rnd;

  int RND::max_int() const { return RAND_MAX; }
  int RND::rnd_int() const { return rand(); }

  //--- tst.cc ---

  #include <iostream.h>

  int main()
  {
    cout << rnd() << endl;
    return 0;
  }

Das kann man sicher noch besser machen, aber fuer den Anfang sollte es
reichen...
--
dietmar.kuehl@uni-konstanz.de
http://www.informatik.uni-konstanz.de/~kuehl
I am a realistic optimist - that's why I appear to be slightly pessimistic
Technical Details
Message-ID: <4jtvge$dgt@news.BelWue.DE>
Path: arc1-ca.pugleaf.net!news.belwue.de!horn!kuehl
References: <4jrj46$5580@info4.rus.uni-stuttgart.de> <4js3sg$sk@sparcserver.lrz-muenchen.de>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.belwue.de!horn!kuehl
From: kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl)
Newsgroups: de.comp.lang.c++
Subject: Re: Zufallsfunktion unter C++?
Date: 3 Apr 1996 13:46:54 GMT
Organization: Fakultät für Mathematik und Informatik
Distribution: world
Message-ID: <4jtvge$dgt@news.BelWue.DE>
References: <4jrj46$5580@info4.rus.uni-stuttgart.de> <4js3sg$sk@sparcserver.lrz-muenchen.de>
Reply-To: dietmar.kuehl@uni-konstanz.de
NNTP-Posting-Host: horn.informatik.uni-konstanz.de
X-Newsreader: TIN [version 1.2 PL2]