Powerful <random> classes in C++11

Have anybody of you learn new C++11? what are your opinions?

For example I'm working on a roulette project already some time, and those new C++11 classes have made fine addition in my projet :D

For instance here is a sample code to demonstrate the power of new C++11 randomnes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <random>
#include <iostream>
#include <ctime>
#include <vector>
#include <cmath>
using namespace std;


int main()
{
	typedef unsigned long ulong_t;

	// seed_seq initializaiton
	const ulong_t nums[] = {1,2,3,4,5,6,7,8,9,10};
	seed_seq seq(nums, nums + 9);

	// generating sequence for example
	vector<ulong_t> set;
	seq.generate(set.begin(), set.end());


	// engines typedefing
	typedef mersenne_twister_engine<ulong_t, 32, 624, 397, 31,
			                        0x9908b0df, 11,
                                                0xffffffff, 7,
                                                0x9d2c5680, 15,
                                                0xefc60000, 18, 
                                                1812433253> twist;

	typedef linear_congruential<ulong_t, 44, 499, 30> gen;
	typedef subtract_with_carry<ulong_t, 50, 33, 8> sub;


	// creating engines
	independent_bits_engine<knuth_b, 25, ulong_t> fin;
	shuffle_order_engine<ranlux24, 333> fin2;
	discard_block_engine<twist, 99, 94009> fin3;

	// start seeding
	fin.seed(seq);
	fin2.seed(seq);
	fin3.seed(seq);


	// distribution declaration
	uniform_int_distribution<ulong_t> dist(3, 37);
	exponential_distribution<float> dist2(exp(1));
	bernoulli_distribution ber(.7);

	// Output
	cout << "knuth_b	" << '\t' << "ranlux24	" << "twist" << endl;
	for (short i = 0; i < 30; i++)
	{
		clock_t stop = clock() + .5 * CLOCKS_PER_SEC;
		while (clock() < stop);
		cout << dist( fin3 ) << "\t\t" << dist2( fin2 ) << "\t\t" << ber( fin3 ) << endl;
	}

	// end of code
	cout << "\nExecution done...";
	std::cin.get();
	return 0;
}


C++ is going to be petty complex and to powerful don't you think so?
Last edited on
C++ is going to be petty complex


I don't think that's new.

The additions to the standard library are nice, but it's not like we didn't have access to the functionality they provide before. New language feature like lambdas, strongly typed enums etc are IMHO more interesting (emulating their functionality in C++03- was way too much work...).
I thought new in standard, not in general.

what I like the most in stdandard is simplicity like only one namespace, only one string to include a header file and similar, while boost with those long multiple namespaces and long path inclusions and similar is not in my favor, but still we have ot use them, regardles of someones taste of style :(

namespace bst = boost::long::name::spacey::thingy?
Yes :D
This is obviously only and the cleanest way.
Could be worse, could be a C++ programmer obsessed with Mass Effect ;).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

namespace n1{
	namespace n2{
		namespace n3{
			namespace n4{
				namespace n5{
					namespace n6{
						namespace n7{
							template <class Type>
							Type larger (Type shepard, Type krogan)
							{
								if (shepard > krogan)
									return shepard;
								else 
									return krogan;
							}
						}
					}
				}
			}
		}
	}
}

int main()
{
	int number;
	number = n1::n2::n3::n4::n5::n6::n7::larger(7, 24);
	std::cout << number;
	return 0;
}
Last edited on by closed account z6A9GNh0
It's not really new: that <random> library has been available as part of boost since 2000 and as part of the TR1 library extensions since 2005.
Topic archived. No new replies allowed.