RandomReal[] and sampling a distribution
- To: mathgroup at smc.vnet.net
- Subject: [mg110552] RandomReal[] and sampling a distribution
- From: John Stone <stone at geology.washington.edu>
- Date: Fri, 25 Jun 2010 07:27:09 -0400 (EDT)
I hope this is a simple question (perhaps too simple for the archives, which I did search). Thanks in advance for any help. I am trying to use RandomReal[ ] to sample from bins of different widths that span the interval 0 - 1. The bin widths represent the weights I'm assigning to a family of trial solutions in an optimization problem. The aim is to sample the solutions in proportion to their weights using a uniform distribution of random numbers generated by RandomReal[ ]. For a simple example, however, suppose there are 10 equally weighted solutions. My selection process would use some code that looks like: weights = Table[0.1, {10}]; bins = Accumulate[weights]; Select[bins, (# >= RandomReal[] &)][[1]] Assuming the result of RandomReal[ ] is uniformly distributed, I expected this to return 0.1 as frequently as it returns 0.5 or 1, but it seems to return a distribution of values peaked around 0.5 (and seldom returns 0.9 or 1). Graphically, the following I think is equivalent, and gives me a peaked, not a flat distribution: Histogram[ Table[ Select[{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1}, (# >= RandomReal[ ] &)][[1]], {1000}] ] Shouldn't the procedure in my example generate a flat distribution? Or am I missing something really simple? Thanks again if you can help me solve this. -- John Stone Department of Earth and Space Sciences University of Washington Phone: + 1 206 221-6332 Box 351310, 70 Johnson Hall Lab: + 1 206 221-6383 Seattle, WA, 98195-1310, USA Fax: + 1 206 543-0489 Web page: http://depts.washington.edu/cosmolab