Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Procedural Generation in Ruby

Alex Taylor
December 01, 2016

Procedural Generation in Ruby

A lightning talk I did at VanRuby in December 2016. I talked about video games and pseudo-random number generators!

Alex Taylor

December 01, 2016
Tweet

More Decks by Alex Taylor

Other Decks in Technology

Transcript

  1. Procedural Generation
    in Ruby

    View full-size slide

  2. Alex Taylor @mctaylorpants

    View full-size slide

  3. Alex Taylor @mctaylorpants

    View full-size slide

  4. Alex Taylor @mctaylorpants
    566034820233894 =
    Images from noelberry.ca

    View full-size slide

  5. Alex Taylor @mctaylorpants
    rand

    View full-size slide

  6. Alex Taylor @mctaylorpants
    def generate_world(seed = 123)
    end

    View full-size slide

  7. Alex Taylor @mctaylorpants

    View full-size slide

  8. Alex Taylor @mctaylorpants

    View full-size slide

  9. Alex Taylor @mctaylorpants
    rand != ‘truly random’

    View full-size slide

  10. Alex Taylor @mctaylorpants
    PRNG
    ● Pseudo-random number generator
    ● Appears random...
    ● ...but actually deterministic
    rspec --seed 123

    View full-size slide

  11. Alex Taylor @mctaylorpants
    srand
    ● Initializes Ruby’s PRNG
    ● Per-process

    View full-size slide

  12. Alex Taylor @mctaylorpants
    a = [1, 2, 3, 4, 5]
    srand(123)
    a.sample
    => 3
    a.sample
    => 5
    a.sample
    => 3
    a.shuffle
    => [1, 5, 3, 4, 2]
    rand
    => 0.7800277619120791

    View full-size slide

  13. Alex Taylor @mctaylorpants

    View full-size slide

  14. Alex Taylor @mctaylorpants
    a = [1, 2, 3, 4, 5]
    srand(123)
    a.sample
    => 3
    a.sample
    => 5
    a.sample
    => 3
    a.shuffle
    => [1, 5, 3, 4, 2]
    rand
    => 0.7800277619120791

    View full-size slide

  15. Alex Taylor @mctaylorpants
    123

    View full-size slide

  16. Alex Taylor @mctaylorpants
    LOVE ME

    View full-size slide

  17. Alex Taylor @mctaylorpants
    srand(123)
    a = ['n', 't', 'k', 'h', 'a', 's']
    a.shuffle.join
    => “thanks”

    View full-size slide