lectures.alex.balgavy.eu

Lecture notes from university.
git clone git://git.alex.balgavy.eu/lectures.alex.balgavy.eu.git
Log | Files | Refs | Submodules

Randomness.md (844B)


      1 +++
      2 title = 'Randomness'
      3 +++
      4 # Randomness
      5 nothing really behaves randomly in a computer, everything is 100% reproducible
      6 
      7 but possible combinations of state often make it look as if something behaves unpredictably
      8 
      9 if you want real randomness, you need to observe a physical phenomenon (e.g. atmospheric noise)
     10 
     11 true randomness is hard to achieve, but pseudo randomness is enough most of the time
     12 
     13 `int rand()`
     14 - Pseudorandom generator
     15 - produces a sequence of numbers based on algorithm that humans can’t guess any better than by “chance”
     16 - it’s a stateful function
     17 - returns pseudo-random number between 0 and RAND_MAX
     18 - included through `<iostream>` for some reason
     19 - unless seeded, always returns same sequence of numbers
     20 
     21 to seed, use function:
     22 `void srand((unsigned) int seed)`
     23 
     24 best to use with time:
     25 `srand((unsigned) time(0))`