# 算法练习(13):随机连接(1.1.31)

• 绘点
• 随机函数

### 题目

1.1.31 随机连接。编写一段程序，从命令行接受一个整数 N 和 double 值 p(0 到 1 之间)作为参数， 在一个圆上画出大小为 0.05 且间距相等的 N 个点，然后将每对点按照概率 p 用灰线连接。

1.1.31 Random connections. Write a program that takes as command-line arguments an integer N and a double value p (between 0 and 1), plots N equally spaced dots of size .05 on the circumference of a circle, and then, with probability p for each pair of points, draws a gray line connecting them.

### 分析

Random的方法的使用
`StdRandom.uniform()`方法生成0到1之间的数；
`StdDraw.point(0.5, 0.5)`方法可以生成居中的点

``````//设置画笔颜色
StdDraw.setPenColor(StdDraw.RED);
//设置画笔的大小
//画个点
StdDraw.point(0.5, 0.5);
``````

`public static void circle(double x, double y, double radius)`

`StdDraw.circle(0.5, 0.5, 0.5);`

``````Point[] points = new Point[N];
for (int i = 0; i < N; i++)
{
points[i] = new Point(0.5 + 0.5 * Math.cos(angle * i * Math.PI / 180),
0.5 + 0.5 * Math.sin(angle * i * Math.PI / 180));
StdDraw.point(points[i].x, points[i].y);
}
``````

### 答案

``````public class RandomConnectSample {

static class Point {
double x;
double y;
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
}

public static void main(String[] args) {
int N = 10;
double p =  StdRandom.uniform();

//算出角度
double angle = 360.0 / N;
StdDraw.circle(0.5, 0.5, 0.5);

Point[] points = new Point[N];
for (int i = 0; i < N; i++)
{
points[i] = new Point(0.5 + 0.5 * Math.cos(angle * i * Math.PI / 180),
0.5 + 0.5 * Math.sin(angle * i * Math.PI / 180));
StdDraw.point(points[i].x, points[i].y);
}

StdDraw.setPenColor(StdDraw.GRAY);

for (int i = 0; i < N - 1; i++)
{
for (int j = i + 1; j < N; j++)
{
if (StdRandom.bernoulli(p))
{
StdDraw.line(points[i].x, points[i].y, points[j].x, points[j].y);
}
}
}

}

}
``````

### 代码索引

RandomConnectSample.java