0

I want to sample random points between $Cos(x)$ and $Sin(x)$ on $0 \le x\le \frac \pi4$enter image description here

I was able to do graph the function here in Mathematica. I would like to count, and then plot points between those two functions. I have tried the following, however, with no luck.

    counter[n_] := (hitsCount = 0; hitsPoints = {}; 
    Do[{x, y} = {RandomReal[{0, Pi/4}], RandomReal[{0, 1}]}; 
    If[Sin[x] <= y <= Cos[x], 
    hitsCount = hitsCount + 1; 
    hits = AppendTo[hitsPoints, {x, y}]], {i, 1, n}];)
    counter[10000]
    ListPlot[hitsPoints, AspectRatio -> Automatic]

However, instead of a large sample of points of that "pizza" looking shape between Cos(x) and Sin(x), I get a blank plot. Is there an issue with my code?

user12289
  • 311
  • 2
  • 7
  • 1
    Apply http://mathematica.stackexchange.com/questions/57938/how-to-generate-random-points-in-a-region to region = DiscretizeRegion@ImplicitRegion[0 <= x <= Pi/4 && Sin[x] <= y <= Cos[x], {x, y}]. – Michael E2 Apr 23 '15 at 18:41
  • @MichaelE2 Thank you very much for your help. Is there any way that I could get them to appear as individualized points on my screen instead of what appears to be tiles? Also, could I specify the number of points I need? I need 5000. – user12289 Apr 23 '15 at 18:44
  • Aha! Yes, that's perfect. Would you mind sharing the syntax? – user12289 Apr 23 '15 at 18:47
  • Jeez, I forgot I can close plotting questions as duplicate with a single vote. If there are any issues, just let me know. – Michael E2 Apr 23 '15 at 18:51
  • For what its worth, aside from some stylistic issues the code in the question works fine. – george2079 Apr 23 '15 at 19:00

1 Answers1

2

Copying ybeltukov's RegionDistribution from How to generate random points in a region?, we get:

reg = ImplicitRegion[0 <= x <= Pi/4 && Sin[x] <= y <= Cos[x], {x, y}];
region = DiscretizeRegion@reg;
pts = RandomVariate[RegionDistribution[region], 5000]; // AbsoluteTiming
ListPlot[pts, AspectRatio -> Automatic]
(*
  {0.003288, Null}
*)

Mathematica graphics

Michael E2
  • 235,386
  • 17
  • 334
  • 747