{{ message }}

Instantly share code, notes, and snippets.

🌍
🎶 around the world 🕺🎶

# Philipe Fatio fphilipe

🌍
🎶 around the world 🕺🎶
Last active Dec 20, 2015
Monte Carlo Sampling of π

From the Wikipedia article on Monte Carlo method:

Monte Carlo methods (or Monte Carlo experiments) are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results; [...]

Given that the circle and the square have a ratio of areas that is π/4, the value of π can be approximated using a Monte Carlo method:

1. Draw a square on the ground, then inscribe a circle within it.
2. Uniformly scatter some objects of uniform size (grains of rice or sand) over the square.
3. Count the number of objects inside the circle and the total number of objects.
4. The ratio of the two counts is an estimate of the ratio of the two areas, which is π/4. Multiply the result by 4 to estimate π.
Created Nov 12, 2012
Obfuscated ids
View gist:4058176
 module Base62 BASE_CHARS = '0SKVnQFHhGs9qo7p8cRW54duMYNXTCErx2tDmwO3kabfUB1elLvjiIgyJAZ6Pz'.split('') BASE = BASE_CHARS.length BASE_ENCODED_MIN_LENGTH = 1 def self.decode(number) # assure string number = number.to_s.reverse.split('') decoded = 0
Created Aug 21, 2012
Objective-C -hash method clash
View main.m
 // This code demonstrates -hash collision on NSString. Two strings have the same // hash when the first, center, and last 32 chars are identical. The other chars // don't matter at all. // // I had to find this out the hard way when loading images from cache while // using the url string hash as the cache key. Unfortunately the urls were all // gravatar urls, thus first 32 chars were identical. Further, all urls also had // a fallback image url appended as query which covered the 32 // center and end chars. The varying part was between the start and center parts // and thus the hash was identical for all urls.
Created May 11, 2012
API Versioning and Inheritance
View gist:2658685
 module API module V1 class UsersController def index 'v1' end def show 'v1' end end
Created Sep 23, 2011
rake assets:precompile without hitting the DB
View assets.rake
 namespace :assets do # Prepend the assets:precompile_prepare task to assets:precompile. task :precompile => :precompile_prepare # This task will be called before assets:precompile to optimize the # compilation, i.e. to prevent any DB calls. task 'precompile_prepare' do # Without this assets:precompile will call itself again with this var set. # This basically speeds things up. ENV['RAILS_GROUPS'] = 'assets'
Created Sep 21, 2011