Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 2a056d9

Browse files
aQuaaQua
aQua
authored and
aQua
committed
478 finish
1 parent 15e3903 commit 2a056d9

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

.vscode/bookmarks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"path": "$ROOTPATH$/Algorithms/0478.generate-random-point-in-a-circle/generate-random-point-in-a-circle.go",
1414
"bookmarks": [
1515
{
16-
"line": 25,
16+
"line": 24,
1717
"column": 11,
1818
"label": ""
1919
}

Algorithms/0478.generate-random-point-in-a-circle/generate-random-point-in-a-circle.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package problem0478
22

33
import (
4-
"fmt"
5-
"math"
64
"math/rand"
75
)
86

@@ -22,12 +20,14 @@ func Constructor(radius, xCenter, yCenter float64) Solution {
2220
}
2321
}
2422

25-
// RandPoint 返回随机点
23+
// RandPoint 返回圆内的随机点
2624
func (s *Solution) RandPoint() []float64 {
27-
len := math.Sqrt(rand.Float64()) * s.r
28-
deg := rand.NormFloat64() * 2 * math.Pi
29-
x := s.a + len*math.Cos(deg)
30-
y := s.b + len*math.Sin(deg)
31-
fmt.Printf("len = %f, x = %f, y = %f\n", len, x, y)
25+
xFactor, yFactor := 1., 1.
26+
for xFactor*xFactor+yFactor*yFactor > 1 {
27+
xFactor = 2*rand.Float64() - 1
28+
yFactor = 2*rand.Float64() - 1
29+
}
30+
x := s.a + s.r*xFactor
31+
y := s.b + s.r*yFactor
3232
return []float64{x, y}
3333
}

Algorithms/0478.generate-random-point-in-a-circle/generate-random-point-in-a-circle_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Test_RandPoint(t *testing.T) {
1515
p := s.RandPoint()
1616
x, y := p[0], p[1]
1717
actual := math.Sqrt((x-a)*(x-a) + (y-b)*(y-b))
18-
ast.True(actual < r)
18+
ast.True(actual <= r)
1919
}
2020
}
2121

@@ -27,6 +27,6 @@ func Test_RandPoint_2(t *testing.T) {
2727
p := s.RandPoint()
2828
x, y := p[0], p[1]
2929
actual := math.Sqrt((x-a)*(x-a) + (y-b)*(y-b))
30-
ast.True(actual < r)
30+
ast.True(actual <= r)
3131
}
3232
}

0 commit comments

Comments
 (0)