Blog

By Categories    By Tags

Two Cool things about dice!

April 14 2016

Just discover two cool things about dice,
Suppose we have two (fair) 6-sided dice, let $Y$ be the reminder of the product of the two numbers divided by 7, i.e., $Y = X1 \times X2 (\text{ mod } 7)$, guess what the distribution of $Y$ is? Uniform 1,…, 6

If it doesn’t surprise you, now consider two n-sided dice and repeat the above with $Y = X_1 \times X_2 (\text{ mod } n+1)$. Guess what values of $n$ will yield a uniform distribution? It turns out that the values of $n$ are prime numbers minus 1. (As 6 = 7-1 and 7 is a prime number, a regular 6-sided dice has this property).

Switching between pythons using pyenv on OSX

January 19 2015

Installation

From pyenv,

pyenv lets you easily switch between multiple versions of Python

As always, I recommend to use homebrew to install pyenv.
Assuming that you have already installed homebrew, to install pyenv, you need

brew update
brew install pyenv

This will also create a hidden folder under your user directory ~/.pyenv. In most cases,
you don’t have look at this directory.

To enable pyenv, put the following into your ~/.bash_profile or ~/.zshrc if you are using zsh. I strongly recommend zsh. Try to install it via homebrew and give it a try.

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

To enable shell completions, also add this to your shell config file

# for bash shell
if [[ -f /usr/local/opt/pyenv/completions/pyenv.bash ]]; then
    source /usr/local/opt/pyenv/completions/pyenv.bash
fi

# for zsh shell
if [[ -f /usr/local/opt/pyenv/completions/pyenv.zsh ]]; then
    source /usr/local/opt/pyenv/completions/pyenv.zsh
fi

Resetting your shell terminal, and check if pyenv runs. To list supported versions of python

pyenv install --list

For academic use, I strongly recommend anaconda3, e.g., anaconda3-2.1.0. (Also, give a try to pypy3)
Installation is a piece of cake.

pyenv install anaconda3-2.1.0

It may take some time for the installation process to finish.

To show available versions of python

pyenv versions

You should see system and anaconda3-2.1.0. System python is the version of python first found along the PATH variable.


Switch between

  • To activate anaconda3-2.1.0 in the current terminal session.
pyenv shell anaconda3-2.1.0
# check python version
python -V
# Python 3.4.1 :: Anaconda 2.1.0 (x86_64)
  • If you want to use anaconda3-2.1.0 every time you visit a particular project directory, say ~/project.
# unset the shell specific version
pyenv shell --unset
# go to the project directory
cd ~/project
pyenv local anaconda3-2.1.0
# check python version
python -V
# Python 3.4.1 :: Anaconda 2.1.0 (x86_64)

Note: you run python -V outside project, you will see the system python.

  • To use anaconda3-2.1.0 globally, run pyenv global anaconda3-2.1.0. But I don’t recommend this.

Remove pyenv

If you really don’t like pyenv and want to remove all traces of pyenv files. Just remove pyenv from homebrew and remove the ~/.pyenv directory. It will do the job. Of course, you have to also undo the changes made to the ~/.bashrc_profile or ~/.zshrc files.

Git aware prompt

February 12 2014

I love using git via commands. And it is very often that I have to run git status or git branch to check if I am on the right branch or I am doing anything correct. Git aware prompt displays the current branch and status of the current directory.

The code was originated from here but I have made significant amount of changes, for example, adding color support and some bugs fixing.

To use this, add the following to your .bashrc and restart your terminal!

Shortcuts in OSX-Terminal

December 3 2013

There are many useful shortcuts in OSX terminal. You may already know some of
them, but I bet you don’t know all of them. I am providing a cheat sheet of those keyboard shortcuts. I only list those I think are useful, more can be found here. First of all, you may want to enable the meta key.

Read More...

A simple python webserver

October 24 2013

The command has been changed in Python 3: python -m http.server.

Python has a very simple web server which allows you to share a particular directory with a one line command.

python -m SimpleHTTPServer 8000

Index directory can be then accessed by http://<ip address>:8000.

Read More...