Presentation is loading. Please wait.

Presentation is loading. Please wait.

Why they aren't really random

Similar presentations


Presentation on theme: "Why they aren't really random"— Presentation transcript:

1 Why they aren't really random
Random Numbers Why they aren't really random

2 Why Random? Obvious uses Less obvious Games Simulations
Cryptography – shared secret

3 Pseudo Random Numbers Recipe: Start with number
Apply random looking math transformation eg : (num * 1009) % 100 12 8 72 48 32 88 92 28 52 68

4 C++ Random Need C standard library: #include <cstdlib>
rand(): returns value between 0 and RAND_MAX RAND_MAX defined by library, usually ~32000

5 Seed Seed : starting value Same seed = same sequence 12 8 72 48 32 88
92 28 52 68 12 8 72 48 32 88 92 28 52 68 12 8 72 48 32 88 92 28 52 68

6 Random Seed srand(num):
seeds random generator with integer value num Reproducible random : seed with constant srand(12);

7 Random Seed Random sequence requires random seed Sources: System clock
Human input Special device Lavarand Radio noise Special hardware

8 Time From ctime library: #include <ctime>
time(0) returns seconds since Jan 1, 1970

9 Random Seed srand(num):
seeds random generator with integer value num Reproducible random : seed with constant srand(12); Less predictable random : seed with time srand(time(0));

10 C++ Random Range Use % to control range:
0 to 9: int num = rand() % 10; 0 to 29: int num = rand() % 30;

11 C++ Random Range Range Minimum Use + NUM for lower bound
1 to 6: int num = rand() % 6 + 1; 100 to 250: int num = rand() % ; Range Minimum

12 C++ Random Other Random double Divide by max size
Watch out for int division double r = static_cast<double>(rand()) / RAND_MAX;

13 Theoretical Notes Cycles appear when we hit repeat values
Use prime numbers Use larger range of values than you really need 12 8 72 48 32 88 92 28 52 68

14 Bad Tendencies Bad recipes may Cycle easily
Favor part of the number space Show patterns High always followed by low

15 C++ Random Numbers Aren't Good
Algorithm is weak Poor distribution Limited range Serious about randomness? Use an add on library


Download ppt "Why they aren't really random"

Similar presentations


Ads by Google