// File: iZhangHui[P]CppPrimer/CH17_SpecializedLibraryFacilities/examples/rand1.cc
// Function: main
int main()
{
	default_random_engine e;  // generates random unsigned integers
	for (size_t i = 0; i < 10; ++i)
		// e() "calls" the object to produce the next random number
		cout << e() << " ";
	cout << endl;

	// uniformly distributed from 0 to 9 inclusive
	uniform_int_distribution<unsigned> u(0,9);
	default_random_engine e2;  
	for (size_t i = 0; i < 10; ++i)
		// u uses e as a source of numbers
		// each call returns a uniformly distributed value 
		// in the specified range
    	cout << u(e2) << " ";
	cout << endl;

	default_random_engine e3;  
	// generates normally distributed doubles 
	// with mean 100, standard deviation 15
	normal_distribution<> n(100,15);  
	for (size_t i = 0; i < 10; ++i)
    	cout << n(e3) << " ";
	cout << endl;

	// bad, but common way to generate random numbers in a given range
	for (size_t i = 0; i < 10; ++i)
		// r will be a between 0 and 9, but randomness is compromised
		cout << rand()%10 << " ";
	cout << endl;

	default_random_engine e4;
	// uniformly distributed from 0 to 9 inclusive
	uniform_int_distribution<unsigned> u2(0,9);
	for (size_t i = 0; i < 10; ++i)
    	cout << u2(e4) << " ";
	cout << endl;

	// seeding the engine causes it to generate different numbers 
	// on different executions
	default_random_engine e5(time(0));  
	for (size_t i = 0; i < 10; ++i)
    	cout << u2(e5) << " ";
	cout << endl;

    // assuming shorts have 16 bits
    uniform_int_distribution<short> us1;     // values from 0 to 32767
    uniform_int_distribution<short> us2(42); // values from 42 to 32767
    cout << "min: " << us1.min() << " max: " << us1.max() << endl;
    cout << "min: " << us2.min() << " max: " << us2.max() << endl;

    default_random_engine e6;
    cout << "min: " << e6.min() << " max: " << e6.max() << endl;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movl $0x0, -0x4(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x16a0
movq $0x0, -0x18(%rbp)
cmpq $0xa, -0x18(%rbp)
jae 0x12bf
leaq -0x10(%rbp), %rdi
callq 0x16c0
movq %rax, %rsi
movq 0x3d3b(%rip), %rdi      # 0x4fd8
callq 0x1030
movq %rax, %rdi
leaq 0x1db8(%rip), %rsi      # 0x3064
callq 0x10c0
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x1283
movq 0x3d12(%rip), %rdi      # 0x4fd8
movq 0x3cfb(%rip), %rsi      # 0x4fc8
callq 0x10d0
leaq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0x16f0
leaq -0x28(%rbp), %rdi
callq 0x16a0
movq $0x0, -0x30(%rbp)
cmpq $0xa, -0x30(%rbp)
jae 0x1332
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1720
movl %eax, %esi
movq 0x3cc8(%rip), %rdi      # 0x4fd8
callq 0x10e0
movq %rax, %rdi
leaq 0x1d45(%rip), %rsi      # 0x3064
callq 0x10c0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x12f3
movq 0x3c9f(%rip), %rdi      # 0x4fd8
movq 0x3c88(%rip), %rsi      # 0x4fc8
callq 0x10d0
leaq -0x38(%rbp), %rdi
callq 0x16a0
leaq -0x58(%rbp), %rdi
movsd 0x1cae(%rip), %xmm0     # 0x3008
movsd 0x1cae(%rip), %xmm1     # 0x3010
callq 0x1750
movq $0x0, -0x60(%rbp)
cmpq $0xa, -0x60(%rbp)
jae 0x13ac
leaq -0x58(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x17a0
movq 0x3c4e(%rip), %rdi      # 0x4fd8
callq 0x1110
movq %rax, %rdi
leaq 0x1ccb(%rip), %rsi      # 0x3064
callq 0x10c0
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x136f
movq 0x3c25(%rip), %rdi      # 0x4fd8
movq 0x3c0e(%rip), %rsi      # 0x4fc8
callq 0x10d0
movq $0x0, -0x68(%rbp)
cmpq $0xa, -0x68(%rbp)
jae 0x1406
callq 0x1040
movl $0xa, %ecx
cltd
idivl %ecx
movl %edx, %esi
movq 0x3bf4(%rip), %rdi      # 0x4fd8
callq 0x1100
movq %rax, %rdi
leaq 0x1c71(%rip), %rsi      # 0x3064
callq 0x10c0
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x13c7
movq 0x3bcb(%rip), %rdi      # 0x4fd8
movq 0x3bb4(%rip), %rsi      # 0x4fc8
callq 0x10d0
leaq -0x70(%rbp), %rdi
callq 0x16a0
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0x16f0
movq $0x0, -0x80(%rbp)
cmpq $0xa, -0x80(%rbp)
jae 0x1479
leaq -0x78(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1720
movl %eax, %esi
movq 0x3b81(%rip), %rdi      # 0x4fd8
callq 0x10e0
movq %rax, %rdi
leaq 0x1bfe(%rip), %rsi      # 0x3064
callq 0x10c0
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x143a
movq 0x3b58(%rip), %rdi      # 0x4fd8
movq 0x3b41(%rip), %rsi      # 0x4fc8
callq 0x10d0
xorl %eax, %eax
movl %eax, %edi
callq 0x10b0
movq %rax, %rsi
leaq -0x88(%rbp), %rdi
callq 0x17d0
movq $0x0, -0x90(%rbp)
cmpq $0xa, -0x90(%rbp)
jae 0x14fa
leaq -0x78(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x1720
movl %eax, %esi
movq 0x3b06(%rip), %rdi      # 0x4fd8
callq 0x10e0
movq %rax, %rdi
leaq 0x1b83(%rip), %rsi      # 0x3064
callq 0x10c0
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x14af
movq 0x3ad7(%rip), %rdi      # 0x4fd8
movq 0x3ac0(%rip), %rsi      # 0x4fc8
callq 0x10d0
leaq -0x94(%rbp), %rdi
callq 0x1800
leaq -0x98(%rbp), %rdi
movl $0x2a, %esi
movl $0x7fff, %edx           # imm = 0x7FFF
callq 0x1830
movq 0x3aa2(%rip), %rdi      # 0x4fd8
leaq 0x1b23(%rip), %rsi      # 0x3060
callq 0x10c0
movq %rax, -0xd0(%rbp)
leaq -0x94(%rbp), %rdi
callq 0x1870
movq -0xd0(%rbp), %rdi
movswl %ax, %esi
callq 0x10a0
movq %rax, %rdi
leaq 0x1af8(%rip), %rsi      # 0x3066
callq 0x10c0
movq %rax, -0xc8(%rbp)
leaq -0x94(%rbp), %rdi
callq 0x1890
movq -0xc8(%rbp), %rdi
movswl %ax, %esi
callq 0x10a0
movq %rax, %rdi
movq 0x3a29(%rip), %rsi      # 0x4fc8
callq 0x10d0
movq 0x3a2d(%rip), %rdi      # 0x4fd8
leaq 0x1aae(%rip), %rsi      # 0x3060
callq 0x10c0
movq %rax, -0xc0(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x1870
movq -0xc0(%rbp), %rdi
movswl %ax, %esi
callq 0x10a0
movq %rax, %rdi
leaq 0x1a83(%rip), %rsi      # 0x3066
callq 0x10c0
movq %rax, -0xb8(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x1890
movq -0xb8(%rbp), %rdi
movswl %ax, %esi
callq 0x10a0
movq %rax, %rdi
movq 0x39b4(%rip), %rsi      # 0x4fc8
callq 0x10d0
leaq -0xa0(%rbp), %rdi
callq 0x16a0
movq 0x39ac(%rip), %rdi      # 0x4fd8
leaq 0x1a2d(%rip), %rsi      # 0x3060
callq 0x10c0
movq %rax, -0xb0(%rbp)
callq 0x18b0
movq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x1030
movq %rax, %rdi
leaq 0x1a09(%rip), %rsi      # 0x3066
callq 0x10c0
movq %rax, -0xa8(%rbp)
callq 0x18c0
movq -0xa8(%rbp), %rdi
movq %rax, %rsi
callq 0x1030
movq %rax, %rdi
movq 0x3941(%rip), %rsi      # 0x4fc8
callq 0x10d0
xorl %eax, %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)