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 Slide

  2. Alex Taylor @mctaylorpants

    View Slide

  3. Alex Taylor @mctaylorpants

    View Slide

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

    View Slide

  5. Alex Taylor @mctaylorpants
    rand

    View Slide

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

    View Slide

  7. Alex Taylor @mctaylorpants

    View Slide

  8. Alex Taylor @mctaylorpants

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 Slide

  13. Alex Taylor @mctaylorpants

    View 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 Slide

  15. Alex Taylor @mctaylorpants
    123

    View Slide

  16. Alex Taylor @mctaylorpants
    LOVE ME

    View Slide

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

    View Slide