本文整理汇总了Java中edu.princeton.cs.introcs.StdRandom.uniform方法的典型用法代码示例。如果您正苦于以下问题:Java StdRandom.uniform方法的具体用法?Java StdRandom.uniform怎么用?Java StdRandom.uniform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.princeton.cs.introcs.StdRandom
的用法示例。
在下文中一共展示了StdRandom.uniform方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
* Unit tests the <tt>Interval2D</tt> data type.
*/
public static void main(String[] args) {
double xlo = Double.parseDouble(args[0]);
double xhi = Double.parseDouble(args[1]);
double ylo = Double.parseDouble(args[2]);
double yhi = Double.parseDouble(args[3]);
int T = Integer.parseInt(args[4]);
Interval1D xinterval = new Interval1D(xlo, xhi);
Interval1D yinterval = new Interval1D(ylo, yhi);
Interval2D box = new Interval2D(xinterval, yinterval);
box.draw();
Counter counter = new Counter("hits");
for (int t = 0; t < T; t++) {
double x = StdRandom.uniform(0.0, 1.0);
double y = StdRandom.uniform(0.0, 1.0);
Point2D p = new Point2D(x, y);
if (box.contains(p)) counter.increment();
else p.draw();
}
StdOut.println(counter);
StdOut.printf("box area = %.2f\n", box.area());
}
示例2: simple
import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
* Returns a random simple digraph containing <tt>V</tt> vertices and <tt>E</tt> edges.
* @param V the number of vertices
* @param E the number of vertices
* @return a random simple digraph on <tt>V</tt> vertices, containing a total
* of <tt>E</tt> edges
* @throws IllegalArgumentException if no such simple digraph exists
*/
public static Digraph simple(int V, int E) {
if (E > (long) V*(V-1)) throw new IllegalArgumentException("Too many edges");
if (E < 0) throw new IllegalArgumentException("Too few edges");
Digraph G = new Digraph(V);
SET<Edge> set = new SET<Edge>();
while (G.E() < E) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
Edge e = new Edge(v, w);
if ((v != w) && !set.contains(e)) {
set.add(e);
G.addEdge(v, w);
}
}
return G;
}
示例3: dag
import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
* Returns a random simple DAG containing <tt>V</tt> vertices and <tt>E</tt> edges.
* Note: it is not uniformly selected at random among all such DAGs.
* @param V the number of vertices
* @param E the number of vertices
* @return a random simple DAG on <tt>V</tt> vertices, containing a total
* of <tt>E</tt> edges
* @throws IllegalArgumentException if no such simple DAG exists
*/
public static Digraph dag(int V, int E) {
if (E > (long) V*(V-1) / 2) throw new IllegalArgumentException("Too many edges");
if (E < 0) throw new IllegalArgumentException("Too few edges");
Digraph G = new Digraph(V);
SET<Edge> set = new SET<Edge>();
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
while (G.E() < E) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
Edge e = new Edge(v, w);
if ((v < w) && !set.contains(e)) {
set.add(e);
G.addEdge(vertices[v], vertices[w]);
}
}
return G;
}
示例4: simple
import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
* Returns a random simple graph containing <tt>V</tt> vertices and <tt>E</tt> edges.
* @param V the number of vertices
* @param E the number of vertices
* @return a random simple graph on <tt>V</tt> vertices, containing a total
* of <tt>E</tt> edges
* @throws IllegalArgumentException if no such simple graph exists
*/
public static Graph simple(int V, int E) {
if (E > (long) V*(V-1)/2) throw new IllegalArgumentException("Too many edges");
if (E < 0) throw new IllegalArgumentException("Too few edges");
Graph G = new Graph(V);
SET<Edge> set = new SET<Edge>();
while (G.E() < E) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
Edge e = new Edge(v, w);
if ((v != w) && !set.contains(e)) {
set.add(e);
G.addEdge(v, w);
}
}
return G;
}
示例5: timeTrial
import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
* Returns the amount of time to call <tt>ThreeSum.count()</tt> with <em>N</em>
* random 6-digit integers.
* @param N the number of integers
* @return amount of time (in seconds) to call <tt>ThreeSum.count()</tt>
* with <em>N</em> random 6-digit integers
*/
public static double timeTrial(int N) {
int MAX = 1000000;
int[] a = new int[N];
for (int i = 0; i < N; i++) {
a[i] = StdRandom.uniform(-MAX, MAX);
}
Stopwatch timer = new Stopwatch();
int cnt = ThreeSum.count(a);
return timer.elapsedTime();
}
示例6: main
import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
public static void main(String[] args) {
// read in bipartite network with 2N vertices and E edges
// we assume the vertices on one side of the bipartition
// are named 0 to N-1 and on the other side are N to 2N-1.
int N = Integer.parseInt(args[0]);
int E = Integer.parseInt(args[1]);
int s = 2*N, t = 2*N + 1;
FlowNetwork G = new FlowNetwork(2*N + 2);
for (int i = 0; i < E; i++) {
int v = StdRandom.uniform(N);
int w = StdRandom.uniform(N) + N;
G.addEdge(new FlowEdge(v, w, Double.POSITIVE_INFINITY));
StdOut.println(v + "-" + w);
}
for (int i = 0; i < N; i++) {
G.addEdge(new FlowEdge(s, i, 1.0));
G.addEdge(new FlowEdge(i + N, t, 1.0));
}
// compute maximum flow and minimum cut
FordFulkerson maxflow = new FordFulkerson(G, s, t);
StdOut.println();
StdOut.println("Size of maximum matching = " + (int) maxflow.value());
for (int v = 0; v < N; v++) {
for (FlowEdge e : G.adj(v)) {
if (e.from() == v && e.flow() > 0)
StdOut.println(e.from() + "-" + e.to());
}
}
}
示例7: main
import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
* Unit tests the point data type.
*/
public static void main(String[] args) {
int x0 = Integer.parseInt(args[0]);
int y0 = Integer.parseInt(args[1]);
int N = Integer.parseInt(args[2]);
StdDraw.setCanvasSize(800, 800);
StdDraw.setXscale(0, 100);
StdDraw.setYscale(0, 100);
StdDraw.setPenRadius(.005);
Point2D[] points = new Point2D[N];
for (int i = 0; i < N; i++) {
int x = StdRandom.uniform(100);
int y = StdRandom.uniform(100);
points[i] = new Point2D(x, y);
points[i].draw();
}
// draw p = (x0, x1) in red
Point2D p = new Point2D(x0, y0);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setPenRadius(.02);
p.draw();
// draw line segments from p to each point, one at a time, in polar order
StdDraw.setPenRadius();
StdDraw.setPenColor(StdDraw.BLUE);
Arrays.sort(points, p.POLAR_ORDER);
for (int i = 0; i < N; i++) {
p.drawTo(points[i]);
StdDraw.show(100);
}
}
示例8: FlowNetwork
import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
* Initializes a random flow network with <tt>V</tt> vertices and <em>E</em> edges.
* The capacities are integers between 0 and 99 and the flow values are zero.
* param V the number of vertices
* param E the number of edges
* @throws java.lang.IllegalArgumentException if <tt>V</tt> < 0
* @throws java.lang.IllegalArgumentException if <tt>E</tt> < 0
*/
public FlowNetwork(int V, int E) {
this(V);
if (E < 0) throw new IllegalArgumentException("Number of edges must be nonnegative");
for (int i = 0; i < E; i++) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
double capacity = StdRandom.uniform(100);
addEdge(new FlowEdge(v, w, capacity));
}
}