Getting random position based in a circle

I’m working on a personal project with crosshairs, and I haven’t found a good way to get a random position in between the crosshair based in a circle.

Based on what I’ve read, I believe I got all of the math down correctly.

Here is my function:

function Crosshair:getRandomPointInsideCrosshair(offset)
	local r = offset * math.sqrt(math.random())
	local theta = math.random() * 2 * math.pi
	local x = r * math.cos(theta)
	local y = r * math.sin(theta)
	return x,y
end

The “offset” argument that is given with the function is the crosshair position (in offset) from the center of the screen.

Here is an example of what’s happening, not sure what is happening to cause this so I decided to come to the dev forum to ask:

Any response is highly appreciated if you could help!

And before anyone says anything, yes I have looked at other topics, and yes I have done my fair share of research.

Besides adjusting the “offset” parameter I couldn’t find anything wrong with the function, it works fine for me. I also don’t fully understand your issue. I did this with the function to get better results:

function getRandomPointInsideCrosshair(offset)
	local r = offset * math.sqrt(math.random()) / 2
	local theta = math.random() * 2 * math.pi
	local x = r * math.cos(theta)
	local y = r * math.sin(theta)
	return x,y
end

after running that a lot it gives me this:
image

So from what I’m seeing this works as intended?

EDIT: in the picture it creates a small square frame each time its ran, this is the outcome of running it hundreds of times.

2 Likes