Given an analytical expression for probability density distribution(PDF), one can use this code to generate a sample of 1-D random numbers. One can just run rand_generator to
function [random_vector] = rand_generator(myfun,xmin,xmax,number,mode_switch)
With a recent improvement, this code now can generate 100,000 samples within 1 second on my laptop.
Here, myfun is a the PDF, xmin and xmax is the range for the variable, while number is the total number for the sample. mode_switch can be set to either 'fast' or 'slow'. This is the only optional argument, with default value set to 'fast'.
The process create a array, then a distinctive calculate the PDF use the given function, following this, the cumulative density function is calculated. An automated check is done in case that your xmin and xmax are set to far and may duce waste of calculation time, or duce problem in worse case.
Then an interpretation is done, so that a random number from U[0,1] is mapped to the cdf, and so this sample will follow the distribution of the desired function. The interpretation process use linear or spline, depending on whether 'fast' or 'slow' is used.
I write this code with git, if you are interested, you can further develop this, or contact me if any bug found. I may further improve this code so that the input could be an array instead of a function, but I feel not that motivated, if anyone specify this requirement, I'll be more than happy to do this.