Random C ++ Function

STL C++ , , , , . . Boost. , Boost , C++11. .

Pseudo-random generator




rand() srand() . ( ). .

( ). -, . .

A truly random generator




Dice as a symbol of chance




. random C++, Boost, . () . . . , :





#include <random>
int roll_a_dice() {
	std::default_random_engine 			e{}; //  
	std::uniform_int_distribution<int>	d{1, 6} //      
	return d(e);
}

      
      



, random, , . , .

return 1 + e() % 6;
      
      



. C++ . Boost C++11 . , – , , . , , .

seed

, , . .

std::default_random_engine e1; //    
std::default_random_engine e2{}; //    
      
      



2 . . .

std::default_random_engine e3{31255}; //  31255
      
      



«31255» - seed (, ) - , . , seed , . decltype(e()), result_of, typename.





?

, , , . , , , . – .

, , . seed. seed time(0) ctime. , , 1 00 00 00 , 1970 UTC.

. . , - .

Random_device –

Random numbers




. . , . Random_device . ( .-.) . , , ..

, . random_device . , , . .

random_device seed

std::random_device			rd{};
std::default_random_engine	e{ rd() };

      
      



. , rd.

, :

e.seed(15027);	//  
e.seed(); 	//   
e.seed( rd() );	//  

      
      



:

(engine) – , .

(distirbution) – , , , , :

  • (uniform);
  • - (normal);
  • (binomial) . .

C++.

  1. default_random_engine, . , , , .
  2. 9 . , . Mersenne twister engines mt19937 ( 32- ) mt19937_64 ( 64- ). . .
  3. , .
Normal distribution




. 20 . random C++ [a, b] - uniform_int_distribution. : uniform_real_distribution a b . , [a, b]. 20 C++ .

, . a b. (geometric_distribution) p.

, . int real. , , (bernoulli_distribution) bool. , , .

Gamma distribution




. . C++.

: Random .Net

.Net framework Random . Random number ++/CLI.

, Visual Studio , System .

.net CLR. .1) windows console app, CLR - Console application CLR ( CLR).2) CLR : ( "", "") -> -> -> -> " (CLR)" " CLR- (/clr)".

#include "stdafx.h"
#include <iostream>

//using namespace System;

int main(array<System::String ^> ^args)
{
	System::Random ^rnd1 = gcnew System::Random(); // ,     
	std::cout << rnd1->Next() << "\n"; //   
	
	int upper = 50;
	std::cout << rnd1->Next(upper) << "\n"; //      upper
	
	int a = -1000; int b = -500;
	std::cout << rnd1->Next(a, b) << "\n"; //     [a, b]

	int seed = 13977;
	System::Random ^rnd2 = gcnew System::Random(seed); //   seed
	std::cout << rnd2->Next(500, 1000) << "\n"; //          .
	
	std::cout << std::endl;

    return 0;
}

      
      



Random Next C++/CLI.

, .net , C++/CLI Common Language Infrastructure. , C++ .Net.

, .

#include <iostream>
#include <random>
#include <ctime>
int main() {
	std::mt19937 e1;
	e1.seed(time(0));
	std::cout << e1() << std::endl;

	std::mt19937 e2(time(0));

	std::mt19937 e3{};
	std::uniform_int_distribution<int> uid1(5, 10), uid2(1, 6);
	std::cout << uid1(e2) << ", " << uid2(e3) << std::endl;

	std::default_random_engine e4{};
	std::uniform_real_distribution<double> urd(0.5, 1.2);
	std::normal_distribution<double> nd(5.0, 2.0); //     5.0    2.0
	std::cout << urd(e4) << ", " << nd(e4) << std::endl;

	std::cout << std::endl;
	system("pause");
	return 0;
}
      
      



. rand(), . STL random, .Net Framework - Random . rand , . . , .




All Articles