Take the 2-minute tour ×
Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. It's 100% free, no registration required.

I want to make my python program fast by using cython, but my inner loop is still making slow python calls to the random number generator! Several years ago this same issue was raised by someone on sage-support and there seemed to be no good solution at that time. It is not convenient for me to pre-generate a long list of random samples because I am actually sampling from various distributions in a way that is conditional on previous samples.

Here's a blog post explaining how this was kludged by connecting from cython to gsl:

And a stackoverflow post by someone trying to implement the gsl kludge:

share|improve this question
I don't understand your question. Cython + GSL RNG is exactly how I would implement this. What would you like to differently? –  Aron Ahmadia Sep 4 '12 at 15:53
@Aron: Maybe that is the answer to my question -- that Cython + GSL RNG is still the best way to do it. What would I like to do differently? I would like to avoid the extra gsl dependency and avoid the boilerplate involved in linking to it, but I understand that the technology for this may not yet exist. But I am optimistic that we are working towards it, for example with projects like github.com/twiecki/CythonGSL . –  none Sep 4 '12 at 16:30

2 Answers 2

Cython makes code faster by removing the type ambiguity. Since random.py is a pure python module, you can just copy it and add the types to the functions you need. Then cython can optimize the dynamic overhead away.

share|improve this answer
I am actually using numpy.random but this is an interesting suggestion to copy and cythonize the code in the packages that I am using. –  none Sep 6 '12 at 18:30

Following aterrel's suggestion, you could use pyximport to automatically compile the random module:

import pyximport

import random

However, this still will not make it as fast as it would be if you declared static types for the variables in Cython.

share|improve this answer
Welcome to the site, Jim! Thanks for the nice out-of-the-starting-gate input :) –  Aron Ahmadia Nov 29 '12 at 10:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.