Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Rando
Search
Tim Uruski
May 14, 2014
Programming
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rando
A lightning talk in which we learn a bit about Ruby by working with random values.
Tim Uruski
May 14, 2014
Transcript
49 slides
Rando # Ruby v2.1.1 Random.srand 123 1
four random things 2
pick a random number between 100 and 200 3
(rand * 100 + 100).to_i => 169 4
(rand * 100 + 100).to_i => 169 NO! 5
rand(100..200) => 166 6
rand(*args) 7
how many kinds of random does Ruby have? 8
ObjectSpace .each_object .select { |obj| obj.respond_to?(:rand) } 9
Kernel Random #<Random:0x007fed39068820> 10
Kernel Random #<Random:0x007fed39068820> Random::DEFAULT => #<Random:0x007fed39068820> 11
Kernel Random #<Random:0x007fed39068820> 12
rand == Kernel.rand Random.rand == Random::DEFAULT.rand Kernel.rand != Random.rand 13
Kernel.rand => 0.6964691855978616 Random.rand => 0.6964691855978616 14
Kernel.rand(100) => 66 Random.rand(100) => 66 15
Kernel.rand(0..100) => 66 Random.rand(0..100) => 66 16
Kernel.rand(100.0) => 66 Random.rand(100.0) => 69.64691855978616 17
Kernel.rand(-100.0) => 66 Random.rand(-100.0) => ArgumentError 18
know your rand 19
generate a list of random numbers 20
(0..20).map { rand } => [0.696469, 0.286139, ...] 21
(0..20).map { rand } => [0.696469, 0.286139, ...] NO! 22
Array.new(20) { rand } => [0.696469, 0.286139, ...] 23
Array.new(*args) 24
Array.new(size, obj, &generator) 25
Array.new(3) => [nil, nil, nil] 26
Array.new(3, 0) => [0, 0, 0] 27
Array.new(3) { |n| n } => [0, 1, 2] 28
know your primitives 29
pick a random subset of elements from an array 30
users = %w[alice bob carol] subset = Array.new(2) { users[rand
* users.length] } => ['bob', 'alice'] 31
users = %w[alice bob carol] subset = Array.new(2) { users[rand
* users.length] } => ['bob', 'alice'] NO! 32
users = %w[alice bob carol] users.sample(2) => ['bob', 'carol'] 33
Array#sample(*args) 34
Array#sample(size, random: rng) 35
a = [1,2,3,4,5] a.sample => 3 36
a = [1,2,3,4,5] a.sample(3) => [3, 1, 5] 37
a = [1,2,3,4,5] a.sample(1) => [3] 38
a = [1,2,3,4,5] rng = Random.new(234) a.sample(random: rng) => 1
39
a = [1,2,3,4,5] rng = Random.new(234) a.sample(3, random: rng) =>
[1, 5, 2] 40
a = [1,2,3,4,5] rng = Random.new(234) a.shuffle(random: rng) => [3,
2, 5, 4, 1] 41
know your primitives (seriously) 42
generate a secure token from random values 43
token = '' 40.times do token << rand(0..255).to_s(16) end =>
'fe6d7e42dc62e6115...' 44
token = '' 40.times do token << rand(0..255).to_s(16) end =>
'fe6d7e42dc62e6115...' NO! 45
require 'securerandom' token = SecureRandom.hex(40) => '6011e8861f024cb75...' 46
require 'securerandom' token = SecureRandom.hex(40) => '6011e8861f024cb75...' => '8f90ea2187b50cb52...' =>
'cad2eb20a39939601...' => '272a822296de5b249...' => 'ad65839e646c27252...' 47
know your security (seriously) 48
hopefully you didn't learn anything 49
More Decks by Tim Uruski
See All by Tim Uruski
Exceptional Ruby
timuruski
0
110
Structure and Interpretation of Ruby Programs
timuruski
1
270
Rack and Middleware
timuruski
0
230
From Whence Rubygems - Mark 2
timuruski
0
180
The Waiting Game
timuruski
0
130
From Whence Rubygems
timuruski
1
110
Other Decks in Programming
See All in Programming
The Cost of the DOM
ypresto
2
180
AIがコードを書いてくれる時代、我々は何を確かめるのか
yosuke_furukawa
PRO
13
6.2k
なぜ私たちは「現時点では」KMPの導入を見送ったのか
chigichan24
1
630
Claude Code ハーネスDeep Dive – 君はどこまで制御する? -
takateeen_knj
6
6.3k
作れるエンジニアから、価値を届けるエンジニアへ
ryunakayama
1
790
仕様駆動開発入門 (何を作るかを先に決めて渡す) | 目指せ Kiro マスター 05
inariku
PRO
3
220
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
800
AIコーディング時代のKotest ユニットテストから APIレベルの仕様固定化テストへ
ooga04
0
330
マルチプロダクトSaaSの アプリ一覧を速くする——複数プロダクトにまたがる「権限行脚」を減らすまで #techramen26conf
mktakuya
1
170
クラウドエージェントは、 何がそんなに便利なのか / what makes cloud agents so useful
rkaga
5
980
テストだけで見つかる? Rubyでつくるメソッド発掘器
izawa
0
160
Distance field of Julia set
kinakomoti321
0
330
Featured
See All Featured
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
550
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Code Review Best Practice
trishagee
74
20k
Agile that works and the tools we love
rasmusluckow
331
22k
Ruling the World: When Life Gets Gamed
codingconduct
0
450
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
73
43k
GraphQLとの向き合い方2022年版
quramy
50
15k
Testing 201, or: Great Expectations
jmmastey
46
8.4k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
Making Projects Easy
brettharned
120
6.9k
Statistics for Hackers
jakevdp
799
230k