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

Rando

 Rando

A lightning talk in which we learn a bit about Ruby by working with random values.

Tim Uruski

May 14, 2014
Tweet

More Decks by Tim Uruski

Other Decks in Programming

Transcript

  1. Rando # Ruby v2.1.1 Random.srand 123 1

  2. four random things 2

  3. pick a random number between 100 and 200 3

  4. (rand * 100 + 100).to_i => 169 4

  5. (rand * 100 + 100).to_i => 169 NO! 5

  6. rand(100..200) => 166 6

  7. rand(*args) 7

  8. how many kinds of random does Ruby have? 8

  9. ObjectSpace .each_object .select { |obj| obj.respond_to?(:rand) } 9

  10. Kernel Random #<Random:0x007fed39068820> 10

  11. Kernel Random #<Random:0x007fed39068820> Random::DEFAULT => #<Random:0x007fed39068820> 11

  12. Kernel Random #<Random:0x007fed39068820> 12

  13. rand == Kernel.rand Random.rand == Random::DEFAULT.rand Kernel.rand != Random.rand 13

  14. Kernel.rand => 0.6964691855978616 Random.rand => 0.6964691855978616 14

  15. Kernel.rand(100) => 66 Random.rand(100) => 66 15

  16. Kernel.rand(0..100) => 66 Random.rand(0..100) => 66 16

  17. Kernel.rand(100.0) => 66 Random.rand(100.0) => 69.64691855978616 17

  18. Kernel.rand(-100.0) => 66 Random.rand(-100.0) => ArgumentError 18

  19. know your rand 19

  20. generate a list of random numbers 20

  21. (0..20).map { rand } => [0.696469, 0.286139, ...] 21

  22. (0..20).map { rand } => [0.696469, 0.286139, ...] NO! 22

  23. Array.new(20) { rand } => [0.696469, 0.286139, ...] 23

  24. Array.new(*args) 24

  25. Array.new(size, obj, &generator) 25

  26. Array.new(3) => [nil, nil, nil] 26

  27. Array.new(3, 0) => [0, 0, 0] 27

  28. Array.new(3) { |n| n } => [0, 1, 2] 28

  29. know your primitives 29

  30. pick a random subset of elements from an array 30

  31. users = %w[alice bob carol] subset = Array.new(2) { users[rand

    * users.length] } => ['bob', 'alice'] 31
  32. users = %w[alice bob carol] subset = Array.new(2) { users[rand

    * users.length] } => ['bob', 'alice'] NO! 32
  33. users = %w[alice bob carol] users.sample(2) => ['bob', 'carol'] 33

  34. Array#sample(*args) 34

  35. Array#sample(size, random: rng) 35

  36. a = [1,2,3,4,5] a.sample => 3 36

  37. a = [1,2,3,4,5] a.sample(3) => [3, 1, 5] 37

  38. a = [1,2,3,4,5] a.sample(1) => [3] 38

  39. a = [1,2,3,4,5] rng = Random.new(234) a.sample(random: rng) => 1

    39
  40. a = [1,2,3,4,5] rng = Random.new(234) a.sample(3, random: rng) =>

    [1, 5, 2] 40
  41. a = [1,2,3,4,5] rng = Random.new(234) a.shuffle(random: rng) => [3,

    2, 5, 4, 1] 41
  42. know your primitives (seriously) 42

  43. generate a secure token from random values 43

  44. token = '' 40.times do token << rand(0..255).to_s(16) end =>

    'fe6d7e42dc62e6115...' 44
  45. token = '' 40.times do token << rand(0..255).to_s(16) end =>

    'fe6d7e42dc62e6115...' NO! 45
  46. require 'securerandom' token = SecureRandom.hex(40) => '6011e8861f024cb75...' 46

  47. require 'securerandom' token = SecureRandom.hex(40) => '6011e8861f024cb75...' => '8f90ea2187b50cb52...' =>

    'cad2eb20a39939601...' => '272a822296de5b249...' => 'ad65839e646c27252...' 47
  48. know your security (seriously) 48

  49. hopefully you didn't learn anything 49