# Displaying random numbers as a graph

3 views (last 30 days)
Mustafa Vural on 17 Oct 2020
Commented: Star Strider on 18 Oct 2020
I am generating random numbers with the rejection sampling, like in this picture. How can I see my generated random numbers as points, like in this picture? #### 1 Comment

Adam Danz on 18 Oct 2020
plot()
scatter()

Star Strider on 18 Oct 2020
One approach is to use the inpolygon function:
x = linspace(0, 10);
y = exp(-(x-5).^2)*0.2;
r = rand(1000,2).*[10 0.25];
[in,on] = inpolygon(r(:,1),r(:,2), x, y);
figure
plot(x,y)
hold on
plot(r(:,1), r(:,2),'.r')
plot(r(in,1), r(in,2), '.g')
hold off
producing: Experiment with it to get the result you want.
.

Star Strider on 18 Oct 2020
It appears that the function plots correctly, the only problem being the random dots. (I was going on the data in the plot image you posted.)
This is the reason it is extremely important to post the exact problem you want help with. The example image helps significantly. I will go by it to define my code in the absence of other information.
You will need to adjust the vector elements that multiply the rand matrix. This is defined as:
r = rand(1000,2).*[x_limit y_limit];
so here:
r = rand(1000,2).*[2 1.2];
to create the dots defining their x-values from 0 to 2 and y-values from 0 to 1.2.
Again using my function because I still do not have your parameters:
x = linspace(0, 2);
y = exp(-(x-1).^2*20)*1.15;
r = rand(1000,2).*[2 1.2];
[in,on] = inpolygon(r(:,1),r(:,2), x, y);
figure
plot(x,y)
hold on
plot(r(:,1), r(:,2),'.r')
plot(r(in,1), r(in,2), '.g')
hold off
producing: Note that the plot x-limits and y-limits have changed as well.
Use your own function, and change the rand multiplication vector as necessary to get the result you want.
.
Mustafa Vural on 18 Oct 2020
I understand now, thank you very much! I appreciate it!
Star Strider on 18 Oct 2020
As always, my pleasure!