Python Forum
Optimisation the rejection method of generating random variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimisation the rejection method of generating random variables
#2
I am not sure about the method (area under the curve of the pdf should be equal 1, and what about its value in your case?), but you can increase speed of computations, if you avoid Python loops. In your case, you can do it as follows:

import numpy as np

# --- Instead of while loop:
# start = ... time measurement started .... 
u1 = np.random.uniform(a, b, size=100000)
u2 = np.random.uniform(0, m, size=100000)
var_inds = u2 <= f(u1)
variables = u1[var_inds]
rejected = (~var_inds).sum() 
#end = ... time measurement ended
# some plotting stuff... 
Reply


Messages In This Thread
RE: Optimisation the rejection method of generating random variables - by scidam - Apr-21-2019, 05:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  About the random number generating shang2019 2 3,336 Jan-13-2019, 02:40 AM
Last Post: gitiya
  Dice and Random Variables isuckatcoding 3 81,977 Dec-04-2017, 05:00 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020