Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rando
Search
Tim Uruski
May 14, 2014
Programming
140
0
Share
Rando
A lightning talk in which we learn a bit about Ruby by working with random values.
Tim Uruski
May 14, 2014
More Decks by Tim Uruski
See All by Tim Uruski
Exceptional Ruby
timuruski
0
110
Structure and Interpretation of Ruby Programs
timuruski
1
250
Rack and Middleware
timuruski
0
210
From Whence Rubygems - Mark 2
timuruski
0
160
The Waiting Game
timuruski
0
120
From Whence Rubygems
timuruski
1
100
Other Decks in Programming
See All in Programming
ECR拡張スキャンでSBOMを収集して サプライチェーン攻撃の影響調査を 爆速で終わらせてみた
akihisaikeda
2
210
CLIであることを活かしたGitHub Copilot CLI活用術 / GitHub Copilot CLI Pro Tips & Tricks
nao_mk2
1
1.1k
These Five Tricks Can Make Your Apps Greener, Cheaper, & Nicer
hollycummins
0
240
Oxlintのカスタムルールの現況
syumai
5
850
Sans tests, vos agents ne sont pas fiables
nabondance
0
170
Agentic UI beyond Chats Architecture Patterns & Open Standards @ngMunich 05/2026
manfredsteyer
PRO
0
170
OCRを使ってゲームのアイテムをデータ化する
kishikawakatsumi
0
120
柔軟なPDFレイアウトエディタを支える型システム設計 — Discriminated UnionとConditional Typeの実践
minako__ph
4
1.1k
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
1
440
Moments When Things Go Wrong
aurimas
3
120
1人1案件のプロダクトエンジニア時代に、"プロセス監督"としてチャレンジしたこと
non0113
0
350
Why Laravel apps break—Mastering the fundamentals to keep them maintainable
kentaroutakeda
1
310
Featured
See All Featured
sira's awesome portfolio website redesign presentation
elsirapls
0
260
Bash Introduction
62gerente
615
210k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
180
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
Embracing the Ebb and Flow
colly
88
5.1k
Making the Leap to Tech Lead
cromwellryan
135
9.9k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
290
The Limits of Empathy - UXLibs8
cassininazir
1
340
The Language of Interfaces
destraynor
162
26k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
61
44k
Heart Work Chapter 1 - Part 1
lfama
PRO
7
36k
Transcript
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