파이썬 범위 지정에 대한 고민
본문
nx,ny = 0,0 #시작좌표
for i in range(n):
result = 0
for _ in range(7):
ax = int(random.randrange(1, 16))
ay = int(random.randrange(1, 16))
distance = math.sqrt(abs(ax - nx) **2 + abs(ay - ny) **2)
result += distance #result에 거리 누적
nx = ax #다음거리가 이전거리가 되야하므로 변수에 넣어주기
ny = ay
distance = math.sqrt(abs(ax) **2 + abs(ay) **2) #마지막 좌표에서 원점까지 돌아온 거리도 합산
result += distance # result에 거리 누적
arr1.append(result)
a = np.array([arr1])
AVG = np.mean(a) #시뮬레이션의 평균을 도출
for i in range(n):
result = 0
for _ in range(2):
bx = int(random.randrange(16, 41))
by = int(random.randrange(16, 41))
distance = math.sqrt(abs(bx - nx) **2 + abs(by - ny) **2)
result += distance #result에 거리 누적
nx = bx #다음거리가 이전거리가 되야하므로 변수에 넣어주기
ny = by
distance = math.sqrt(abs(bx) **2 + abs(by) **2) #마지막 좌표에서 원점까지 돌아온 거리도 합산
result += distance # result에 거리 누적
arr2.append(result)
a = np.array([arr2])
AVG = np.mean(a) #시뮬레이션의 평균을 도출
for i in range(n):
result = 0
for _ in range(1):
cx = int(random.randrange(41, 101))
cy = int(random.randrange(41, 101))
distance = math.sqrt(abs(cx - nx) **2 + abs(cy - ny) **2)
result += distance #result에 거리 누적
nx = cx #다음거리가 이전거리가 되야하므로 변수에 넣어주기
ny = cy
distance = math.sqrt(abs(cx) **2 + abs(cy) **2) #마지막 좌표에서 원점까지 돌아온 거리도 합산
result += distance # result에 거리 누적
arr3.append(result)
a = np.array([arr3])
AVG = np.mean(a) #시뮬레이션의 평균을 도출
nx,ny = 0,0 #시작좌표
for i in range(n):
result = 0
for _ in range(7):
ax = int(random.randrange(1, 16))
ay = int(random.randrange(1, 16))
distance = math.sqrt(abs(ax - nx) **2 + abs(ay - ny) **2)
result += distance #result에 거리 누적
nx = ax #다음거리가 이전거리가 되야하므로 변수에 넣어주기
ny = ay
distance = math.sqrt(abs(ax) **2 + abs(ay) **2) #마지막 좌표에서 원점까지 돌아온 거리도 합산
result += distance # result에 거리 누적
arr1.append(result)
a = np.array([arr1])
AVG = np.mean(a) #시뮬레이션의 평균을 도출
for i in range(n):
result = 0
for _ in range(2):
bx = int(random.randrange(16, 41))
by = int(random.randrange(16, 41))
distance = math.sqrt(abs(bx - nx) **2 + abs(by - ny) **2)
result += distance #result에 거리 누적
nx = bx #다음거리가 이전거리가 되야하므로 변수에 넣어주기
ny = by
distance = math.sqrt(abs(bx) **2 + abs(by) **2) #마지막 좌표에서 원점까지 돌아온 거리도 합산
result += distance # result에 거리 누적
arr2.append(result)
a = np.array([arr2])
AVG = np.mean(a) #시뮬레이션의 평균을 도출
for i in range(n):
result = 0
for _ in range(1):
cx = int(random.randrange(41, 101))
cy = int(random.randrange(41, 101))
distance = math.sqrt(abs(cx - nx) **2 + abs(cy - ny) **2)
result += distance #result에 거리 누적
nx = cx #다음거리가 이전거리가 되야하므로 변수에 넣어주기
ny = cy
distance = math.sqrt(abs(cx) **2 + abs(cy) **2) #마지막 좌표에서 원점까지 돌아온 거리도 합산
result += distance # result에 거리 누적
arr3.append(result)
a = np.array([arr3])
AVG = np.mean(a) #시뮬레이션의 평균을 도출
안녕하세요
랜덤으로 x,y 좌표를 뽑아 그 좌표 간의 거리를 구하려고하는데 제가 생각을 잘못했더군요
이 그림 처럼 (15~15) 좌표상에서 7개
(15~15)를 제외한 (40~40) 좌표상에서 2개
(40~40)을 제외한 (100~100)좌표상에서 1개 이걸 도출해내려했는데
바보같이 이걸 도출하는 식이엇엇습니다...
혹시 위에 그림과같은 좌표를 뽑을 만한 식을 구할수있을까요
답변 1
bx = int(random.randrange(16,41)
by = int(random.randrange(16,41)
이걸 이런식으로 만 바꾸면 되는거 아닌가요?
do {
bx = int(random.randrange(1,41)
by = int(random.randrange(1,41)
} while(bx>15 || by>15)
코드는 정확하지 않습니다. ^^
!-->!-->
답변을 작성하시기 전에 로그인 해주세요.