Created
May 21, 2025 16:04
-
-
Save joedf/7f00ed0c4b3f31173bda7ebf2930a2c9 to your computer and use it in GitHub Desktop.
Random points within a circle in python, example output: https://www.desmos.com/calculator/4jdjtilzpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ================================================== | |
# Joachim testing radius pts | |
# 2025-05-21 11:53:08 | |
import math | |
import random | |
def randPoint_radius(radius, origin_x=0, origin_y=0): | |
# modified from https://stackoverflow.com/a/32277202/883015 | |
# radius = random() * radius | |
radius = radius * math.sqrt(random.random()) | |
angleRad = random.random() * 2 * math.pi | |
return (float(origin_x + radius * math.cos(angleRad)), | |
float(origin_y + radius * math.sin(angleRad))) | |
G_TOTAL = 2000 | |
G_RADIUS = 5 | |
print(f'Generating {G_TOTAL} random points within a circle with r = {G_RADIUS}') | |
with open('~radius_results.CSV', 'w') as fp: | |
fp.write('x, y\n') | |
for i in range(0, G_TOTAL): | |
pt = randPoint_radius(G_RADIUS) | |
line = f'{pt[0]}, {pt[1]}' | |
print(f'Point #{i}: \t{line}') | |
fp.write(f'{line}\n') | |
print('done!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment