Last active
August 29, 2015 14:04
-
-
Save stalcottsmith/1b74155cbfe3a23d8573 to your computer and use it in GitHub Desktop.
My go-to solution for random id numbers which programmers frequently need
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# yields an 11 digit number in base-36 | |
# with an equal probability distribution for all digits | |
(0..10).to_a.map {(rand*36).to_i.to_s(36)}.join | |
# this is slightly preferable to the more simple | |
(rand*(36**11)).to_i.to_s(36) | |
# because any number so generated will occasionally be "short" | |
# and because the first digit will follow a non-random distribution | |
# in terms of frequency due to some strange number theory thing | |
# that was only proven in the last decade i think |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, I’ll definitely keep that in mind. A nice thing about UUIDs though is that they don’t include o, i, l and some other chars which makes them a lot more readable. Of course that may not be necessary for everything, but for some use cases it’s great. Also iBeacons :)