sampling integers uniformly efficiently in python using numpy/scipy
- by user248237
I have a problem where depending on the result of a random coin flip, I have to sample a random starting position from a string.  If the sampling of this random position is uniform over the string, I thought of two approaches to do it: one using multinomial from numpy.random, the other using the simple randint function of Python standard lib.  I tested this as follows:
from numpy import *
from numpy.random import multinomial
from random import randint
import time
def use_multinomial(length, num_points):
    probs = ones(length)/float(length)
    for n in range(num_points):
    result = multinomial(1, probs)
def use_rand(length, num_points):
    for n in range(num_points):
    rand(1, length)
def main():
    length = 1700
    num_points = 50000
    t1 = time.time()
    use_multinomial(length, num_points)
    t2 = time.time()
    print "Multinomial took: %s seconds" %(t2 - t1)
    t1 = time.time()
    use_rand(length, num_points)
    t2 = time.time()
    print "Rand took: %s seconds" %(t2 - t1)    
if __name__ == '__main__':
    main()
The output is:
Multinomial took: 6.58072400093 seconds
Rand took: 2.35189199448 seconds
it seems like randint is faster, but it still seems very slow to me.  Is there a vectorized way to get this to be much faster, using numpy or scipy?
thanks.