List of usage examples for org.apache.commons.math.random RandomGenerator nextInt
int nextInt(int n);
From source file:de.tud.kom.p2psim.impl.application.ido.moveModels.RandomPositionDistribution.java
@Override public Point getNextPosition() { RandomGenerator r = Simulator.getRandom(); Point p = new Point(r.nextInt(worldDimensionX), r.nextInt(worldDimensionY)); return p;/*w w w . ja va 2 s.c o m*/ }
From source file:de.tud.kom.p2psim.impl.application.ido.moveModels.RandomMoveModel.java
@Override public Point getNextPosition(IDOApplication app) { RandomGenerator r = Simulator.getRandom(); Point pos = app.getPlayerPosition(); Point newPos;// w w w . j ava2s. c o m if (portal != null) { Point position = portal.portal(app.getPlayerPosition(), app, worldDimensionX, worldDimensionY); // if a portal is used. if (position != null) { return position; } } do { newPos = new Point(pos.x + (r.nextInt(2 * moveSpeedLimit + 1) - moveSpeedLimit), pos.y + (r.nextInt(2 * moveSpeedLimit + 1) - moveSpeedLimit)); } while (newPos.x < 0 || newPos.x > worldDimensionX || newPos.y < 0 || newPos.y > worldDimensionY); setMoveVector(app, newPos); return newPos; }
From source file:lfsom.visualization.clustering.LFSKMeans.java
/** Take random points from the input data as centroids. */ private void initClustersRandomlyOnInstances() { ArrayList<double[]> usedInstances = new ArrayList<double[]>(); RandomGenerator rg = new JDKRandomGenerator(); // FIXME: this is for testing purposes only rg.setSeed(RANDOM_SEED);//from ww w.j av a 2 s . co m // for each cluster for (int clusterIndex = 0; clusterIndex < k; clusterIndex++) { // draw a random input double[] centroid = data[rg.nextInt(data.length - 1)].clone(); while (usedInstances.contains(centroid)) { centroid = data[rg.nextInt(data.length - 1)].clone(); } usedInstances.add(centroid); clusters[clusterIndex] = new LFSCluster(centroid); } }
From source file:org.dishevelled.analysis.GraphGenerators.java
/** * Connect the specified graph randomly with the specified value on all added edges. * * @param <N> graph node type//w w w . ja va2s . c o m * @param <E> graph edge type * @param graph graph to connect, must not be null * @param edgeCount edge count, must be at least zero * @param edgeValues edge values, must not be null * @param random source of randomness, must not be null * @return the specified graph connected randomly with the specified value on all added edges */ public static <N, E> Graph<N, E> connectRandomly(final Graph<N, E> graph, final int edgeCount, final BinaryFunction<N, N, E> edgeValues, final RandomGenerator random) { if (graph == null) { throw new IllegalArgumentException("graph must not be null"); } if (edgeCount < 0) { throw new IllegalArgumentException("edgeCount must be at least zero"); } if (edgeValues == null) { throw new IllegalArgumentException("edgeValues must not be null"); } if (random == null) { throw new IllegalArgumentException("random must not be null"); } List<Node<N, E>> nodes = Lists.asImmutableList(graph.nodes()); for (int i = graph.edgeCount(); i < edgeCount; i++) { Node<N, E> source = nodes.get(random.nextInt(nodes.size())); Node<N, E> target = nodes.get(random.nextInt(nodes.size())); E edgeValue = edgeValues.evaluate(source.getValue(), target.getValue()); graph.createEdge(source, target, edgeValue); } return graph; }
From source file:org.dishevelled.analysis.GraphGenerators.java
/** * Create and return a new randomly connected graph with the specified nodes values * on nodes and values provided by the specified function on edges. * * @param <N> graph node type//w w w .ja va 2 s . c om * @param <E> graph edge type * @param nodeValues list of node values, must not be null * @param edgeCount edge count, must be at least zero * @param edgeValues edge values, must not be null * @param random source of randomness, must not be null * @return a new randomly connected graph with the specified nodes values * on nodes and the specified edge value on all added edges */ public static <N, E> Graph<N, E> connectRandomly(final List<N> nodeValues, final int edgeCount, final BinaryFunction<N, N, E> edgeValues, final RandomGenerator random) { if (nodeValues == null) { throw new IllegalArgumentException("nodeValues must not be null"); } if (edgeCount < 0) { throw new IllegalArgumentException("edgeCount must be at least zero"); } if (edgeValues == null) { throw new IllegalArgumentException("edgeValues must not be null"); } if (random == null) { throw new IllegalArgumentException("random must not be null"); } int n = nodeValues.size(); Graph<N, E> graph = GraphUtils.createGraph(n, edgeCount); List<Node<N, E>> nodes = Lists.createList(n); for (N nodeValue : nodeValues) { nodes.add(graph.createNode(nodeValue)); } for (int i = 0; i < edgeCount; i++) { Node<N, E> source = nodes.get(random.nextInt(n)); Node<N, E> target = nodes.get(random.nextInt(n)); E edgeValue = edgeValues.evaluate(source.getValue(), target.getValue()); graph.createEdge(source, target, edgeValue); } return graph; }
From source file:org.peerfact.impl.util.movement.RandomMovement.java
public PositionVector getNextRandomDelta(PositionVector oldPosition) { RandomGenerator r = Simulator.getRandom(); double[] delta = new double[oldPosition.getDimensions()]; for (int i = 0; i < oldPosition.getDimensions(); i++) { do {/* w w w. j a va2s. c om*/ delta[i] = (r.nextInt(2 * getMoveSpeedLimit() + 1) - getMoveSpeedLimit()); } while (oldPosition.getEntry(i) + delta[i] > getWorldDimension(i) || oldPosition.getEntry(i) + delta[i] < 0); } PositionVector deltaVector = new PositionVector(delta); return deltaVector; }
From source file:org.peerfact.impl.util.positioning.RandomPositionDistribution.java
@Override public PositionVector getNextPosition() { RandomGenerator r = Simulator.getRandom(); double[] vec = new double[getDimensions()]; for (int i = 0; i < getDimensions(); i++) { vec[i] = r.nextInt(worldDimensions[i]); }/*from w w w .ja va 2 s . c om*/ PositionVector position = new PositionVector(vec); return position; }