Generating Random x-y Coordinates within a 2x2 Square Using randn Function

In summary, The speaker is asking for help with using the randn function to generate random x and y values within a 2 x 2 square centered at (0,0). They mention that they must use randn and ask if anyone can help them with achieving this. They also mention the possibility of discarding points outside of the square, but acknowledge that this will affect the normal distribution of the points.
  • #1
strokebow
123
0
Okay... I am using the randn function for a sequence of normally distributed numbers.
I am writing a program whereby I need to use this randn to give me random x and y values whose position r is less than 1

i.e. that is to say that all points fall within a 2 x 2 square centred at (0, 0).
And I MUST use randn

Can anyone help me achieve this.

thanks
 
Physics news on Phys.org
  • #2
Do you need the points to have any particular distribution (normal, uniform, etc.)? You could just discard any generated points that fall outside the 2 x 2 square, but then the points will not have a normal distribution.
 
  • #3


Sure, I can definitely help you with this! The randn function in MATLAB generates a sequence of normally distributed random numbers with a mean of 0 and a standard deviation of 1. To generate random x-y coordinates within a 2x2 square, we can use the following steps:

1. First, we need to define the size of our square, which is 2x2. We can do this by creating two variables, let's say xSize and ySize, and assigning them a value of 2.

2. Next, we need to generate random x and y values using the randn function. We can do this by creating two variables, xCoord and yCoord, and assigning them the output of the randn function multiplied by the size of our square. This will give us a range of values from -2 to 2 for both x and y.

3. Now, we need to make sure that the generated coordinates fall within our square. To do this, we can use an if statement to check if the absolute value of xCoord and yCoord is less than or equal to the size of our square (i.e. 2). If they are, then we can use these coordinates, otherwise, we can generate new coordinates until we get a valid pair.

4. Finally, we can plot these coordinates using the scatter function in MATLAB to visualize our points within the 2x2 square.

Here is an example code that implements these steps:

xSize = 2; % size of the square in x direction
ySize = 2; % size of the square in y direction

xCoord = randn * xSize; % generate random x coordinate
yCoord = randn * ySize; % generate random y coordinate

% check if coordinates fall within the square
if abs(xCoord) <= xSize && abs(yCoord) <= ySize
% plot the coordinates within the square
scatter(xCoord, yCoord);
else
% if coordinates are outside the square, generate new ones until we get a valid pair
while abs(xCoord) > xSize || abs(yCoord) > ySize
xCoord = randn * xSize;
yCoord = randn * ySize;
end
% plot the valid coordinates within the square
scatter(xCoord, yCoord);
end

I hope this helps! Let me know if you have any other questions or
 

What does the randn function do in this scenario?

The randn function generates a random number from a standard normal distribution. In this case, it is used to generate random x-y coordinates within a 2x2 square.

How do you limit the coordinates to be within the 2x2 square?

To limit the coordinates within the 2x2 square, we can use the randn function to generate numbers between -1 and 1, and then scale them by multiplying with 2 to get numbers between -2 and 2. These numbers can then be added to the center point of the square (0,0) to get coordinates within the 2x2 square.

Can the randn function be used to generate coordinates within any sized square?

Yes, the randn function can be used to generate coordinates within any sized square. By scaling the generated numbers and adding them to the center point of the square, we can generate coordinates within the desired square.

How many coordinates can be generated using the randn function?

The number of coordinates that can be generated using the randn function is infinite. The function can continuously generate random numbers from a standard normal distribution, providing an endless number of possible coordinates within the 2x2 square.

How can the generated coordinates be used in scientific experiments or simulations?

The generated coordinates can be used in scientific experiments or simulations that require random sampling or placement within a 2x2 square. They can also be used to create randomized data sets for statistical analysis or to model random movement or patterns within a confined space.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
875
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
10K
  • Programming and Computer Science
Replies
1
Views
625
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
735
Back
Top