Zufallszahlen in C++ (Zufallszahlen in C++), Lektion, page 722680
https://www.purl.org/stefan_ram/pub/zufallszahlen_c++ (permalink) is the canonical URI of this page.
Stefan Ram
C++-Kurs

Zufallszahlen in C++ 

std::random_device r;

// std::default_random_engine e1(r()); // hat nur sehr kurze Periode, hier schlecht

std::mt19937 e1(r()); // besser, größere Periode

std::uniform_int_distribution<int> uniform_dist100(2, 99);

#define _GLIBCXX_USE_RANDOM_TR1
#include <iostream>
#include <ostream>
#include <random>

int main()
{ ::std::default_random_engine default_engine;
::std::uniform_int_distribution< int >dice_distribution{ 1, 6 };
auto const dice =[ & ]()-> int{ return dice_distribution( default_engine ); };
for( int i = 6; i--; )::std::cout << dice() << '\n'; }

1
1
5
3
4
2

#define _GLIBCXX_USE_RANDOM_TR1
#include <iostream>
#include <ostream>
#include <random>
#include <ctime>

int main()
{ ::std::uniform_int_distribution< int >dice_distribution{ 1, 6 };
::std::default_random_engine default_engine( ::std::time( 0 ));
auto const dice =[ & ]()-> int{ return dice_distribution( default_engine ); };
for( int i = 6; i--; )::std::cout << dice() << '\n'; }

6
5
6
1
3
5

#define _GLIBCXX_USE_RANDOM_TR1
#include <random>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <ctime>

int main()
{ ::std::string str{ "abcdefghijklmnopqrstuvwxyz" };
::std::default_random_engine default_engine( ::std::time( 0 ));
::std::shuffle( begin( str ), end( str ), default_engine );
::std::cout << str << "\n"; }

lfpwatxbsydqigmknrvjouhezc

#include <iostream>
#include <ostream>
#include <random>

int main()
{ ::std::cout << ::std::random_device{}.entropy() << "\n"; }

0

Notizen

#define _GLIBCXX_USE_RANDOM_TR1
#include <random>
#include <chrono>

::std::mt19937_64 engine
( static_cast<unsigned>(std::chrono::system_clock::now().time_since_epoch().count()) *
static_cast<unsigned>(std::chrono::system_clock::now().time_since_epoch().count()));
::std::uniform_real_distribution< double >distribution( 0.0, 1.0 );

distribution( engine ); distribution( engine ); distribution( engine );

x = distribution( engine )

|GCC 4.6+ and Clang 3.2+ provide intrinsic functions for

|RdRand when -mrdrnd is specified in the flags,[20] also

|setting __RDRND__ to allow conditional compilation.

en.wikipedia.org/wiki/RdRand

About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email-address of Stefan Ram.   |   A link to the start page of Stefan Ram appears at the top of this page behind the text "Stefan Ram".)  |   Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram722680 stefan_ram:722680 Zufallszahlen in C++ Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd722680, slrprddef722680, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram.
https://www.purl.org/stefan_ram/pub/zufallszahlen_c++