本文整理汇总了Java中edu.princeton.cs.introcs.StdRandom类的典型用法代码示例。如果您正苦于以下问题:Java StdRandom类的具体用法?Java StdRandom怎么用?Java StdRandom使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StdRandom类属于edu.princeton.cs.introcs包,在下文中一共展示了StdRandom类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Reads in a sequence of strings from standard input; quicksorts them;
* and prints them to standard output in ascending order.
* Shuffles the array and then prints the strings again to
* standard output, but this time, using the select method.
*/
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
Quick.sort(a);
show(a);
// shuffle
StdRandom.shuffle(a);
// display results again using select
StdOut.println();
for (int i = 0; i < a.length; i++) {
String ith = (String) Quick.select(a, i);
StdOut.println(ith);
}
}
示例2: 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());
}
示例3: main
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Reads two command-line integers N and T; creates N counters;
* increments T counters at random; and prints results.
*/
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
int T = Integer.parseInt(args[1]);
// create N counters
Counter[] hits = new Counter[N];
for (int i = 0; i < N; i++) {
hits[i] = new Counter("counter" + i);
}
// increment T counters at random
for (int t = 0; t < T; t++) {
hits[StdRandom.uniform(N)].increment();
}
// print results
for (int i = 0; i < N; i++) {
StdOut.println(hits[i]);
}
}
示例4: 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;
}
示例5: 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;
}
示例6: 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;
}
示例7: wheel
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Returns a wheel graph on <tt>V</tt> vertices.
* @param V the number of vertices in the wheel
* @return a wheel graph on <tt>V</tt> vertices: a single vertex connected to
* every vertex in a cycle on <tt>V-1</tt> vertices
*/
public static Graph wheel(int V) {
if (V <= 1) throw new IllegalArgumentException("Number of vertices must be at least 2");
Graph G = new Graph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
// simple cycle on V-1 vertices
for (int i = 1; i < V-1; i++) {
G.addEdge(vertices[i], vertices[i+1]);
}
G.addEdge(vertices[V-1], vertices[1]);
// connect vertices[0] to every vertex on cycle
for (int i = 1; i < V; i++) {
G.addEdge(vertices[0], vertices[i]);
}
return G;
}
示例8: regular
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Returns a uniformly random <tt>k</tt>-regular graph on <tt>V</tt> vertices
* (not necessarily simple). The graph is simple with probability only about e^(-k^2/4),
* which is tiny when k = 14.
* @param V the number of vertices in the graph
* @return a uniformly random <tt>k</tt>-regular graph on <tt>V</tt> vertices.
*/
public static Graph regular(int V, int k) {
if (V*k % 2 != 0) throw new IllegalArgumentException("Number of vertices * k must be even");
Graph G = new Graph(V);
// create k copies of each vertex
int[] vertices = new int[V*k];
for (int v = 0; v < V; v++) {
for (int j = 0; j < k; j++) {
vertices[v + V*j] = v;
}
}
// pick a random perfect matching
StdRandom.shuffle(vertices);
for (int i = 0; i < V*k/2; i++) {
G.addEdge(vertices[2*i], vertices[2*i + 1]);
}
return G;
}
示例9: select
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Rearranges the array so that a[k] contains the kth smallest key;
* a[0] through a[k-1] are less than (or equal to) a[k]; and
* a[k+1] through a[N-1] are greater than (or equal to) a[k].
* @param a the array
* @param k find the kth smallest
*/
public static Comparable select(Comparable[] a, int k) {
if (k < 0 || k >= a.length) {
throw new IndexOutOfBoundsException("Selected element out of bounds");
}
StdRandom.shuffle(a);
int lo = 0, hi = a.length - 1;
while (hi > lo) {
int i = partition(a, lo, hi);
if (i > k) hi = i - 1;
else if (i < k) lo = i + 1;
else return a[i];
}
return a[lo];
}
示例10: path
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Returns a path digraph on <tt>V</tt> vertices.
* @param V the number of vertices in the path
* @return a digraph that is a directed path on <tt>V</tt> vertices
*/
public static Digraph path(int V) {
Digraph G = new Digraph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
for (int i = 0; i < V-1; i++) {
G.addEdge(vertices[i], vertices[i+1]);
}
return G;
}
示例11: binaryTree
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Returns a complete binary tree digraph on <tt>V</tt> vertices.
* @param V the number of vertices in the binary tree
* @return a digraph that is a complete binary tree on <tt>V</tt> vertices
*/
public static Digraph binaryTree(int V) {
Digraph G = new Digraph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
for (int i = 1; i < V; i++) {
G.addEdge(vertices[i], vertices[(i-1)/2]);
}
return G;
}
示例12: cycle
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Returns a cycle digraph on <tt>V</tt> vertices.
* @param V the number of vertices in the cycle
* @return a digraph that is a directed cycle on <tt>V</tt> vertices
*/
public static Digraph cycle(int V) {
Digraph G = new Digraph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
for (int i = 0; i < V-1; i++) {
G.addEdge(vertices[i], vertices[i+1]);
}
G.addEdge(vertices[V-1], vertices[0]);
return G;
}
示例13: bipartite
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Returns a random simple bipartite graph on <tt>V1</tt> and <tt>V2</tt> vertices,
* containing each possible edge with probability <tt>p</tt>.
* @param V1 the number of vertices in one partition
* @param V2 the number of vertices in the other partition
* @param p the probability that the graph contains an edge with one endpoint in either side
* @return a random simple bipartite graph on <tt>V1</tt> and <tt>V2</tt> vertices,
* containing each possible edge with probability <tt>p</tt>
* @throws IllegalArgumentException if probability is not between 0 and 1
*/
public static Graph bipartite(int V1, int V2, double p) {
if (p < 0.0 || p > 1.0)
throw new IllegalArgumentException("Probability must be between 0 and 1");
int[] vertices = new int[V1 + V2];
for (int i = 0; i < V1 + V2; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
Graph G = new Graph(V1 + V2);
for (int i = 0; i < V1; i++)
for (int j = 0; j < V2; j++)
if (StdRandom.bernoulli(p))
G.addEdge(vertices[i], vertices[V1+j]);
return G;
}
示例14: path
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Returns a path graph on <tt>V</tt> vertices.
* @param V the number of vertices in the path
* @return a path graph on <tt>V</tt> vertices
*/
public static Graph path(int V) {
Graph G = new Graph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
for (int i = 0; i < V-1; i++) {
G.addEdge(vertices[i], vertices[i+1]);
}
return G;
}
示例15: binaryTree
import edu.princeton.cs.introcs.StdRandom; //导入依赖的package包/类
/**
* Returns a complete binary tree graph on <tt>V</tt> vertices.
* @param V the number of vertices in the binary tree
* @return a complete binary tree graph on <tt>V</tt> vertices
*/
public static Graph binaryTree(int V) {
Graph G = new Graph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
for (int i = 1; i < V; i++) {
G.addEdge(vertices[i], vertices[(i-1)/2]);
}
return G;
}