Java Code Examples for org.apache.commons.math3.random.RandomGenerator#nextFloat()

The following examples show how to use org.apache.commons.math3.random.RandomGenerator#nextFloat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Polygon.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new random Polygon of the given length.
 */
public static Polygon randomPolygon(int length) {
    final int polygonSize = 4 + 2 * length;

    final RandomGenerator random = GeneticAlgorithm.getRandomGenerator();
    
    Polygon p = new Polygon();
    p.data = new float[polygonSize];

    p.data[0] = random.nextFloat(); // r
    p.data[1] = random.nextFloat(); // g
    p.data[2] = random.nextFloat(); // b
    p.data[3] = FastMath.max(0.2f, random.nextFloat() * random.nextFloat()); // a
    
    float px = random.nextFloat();
    float py = random.nextFloat();
    
    for (int k = 0; k < length; k++) {
        p.data[4 + 2*k] = px + (random.nextFloat() - 0.5f);
        p.data[5 + 2*k] = py + (random.nextFloat() - 0.5f);
    }
    return p;
}
 
Example 2
Source File: Polygon.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new random Polygon of the given length.
 */
public static Polygon randomPolygon(int length) {
    final int polygonSize = 4 + 2 * length;

    final RandomGenerator random = GeneticAlgorithm.getRandomGenerator();
    
    Polygon p = new Polygon();
    p.data = new float[polygonSize];

    p.data[0] = random.nextFloat(); // r
    p.data[1] = random.nextFloat(); // g
    p.data[2] = random.nextFloat(); // b
    p.data[3] = FastMath.max(0.2f, random.nextFloat() * random.nextFloat()); // a
    
    float px = random.nextFloat();
    float py = random.nextFloat();
    
    for (int k = 0; k < length; k++) {
        p.data[4 + 2*k] = px + (random.nextFloat() - 0.5f);
        p.data[5 + 2*k] = py + (random.nextFloat() - 0.5f);
    }
    return p;
}
 
Example 3
Source File: RMatGraph.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void flatMap(BlockInfo<T> blockInfo, Collector<Edge<LongValue, NullValue>> out)
		throws Exception {
	RandomGenerator rng = blockInfo.getRandomGenerable().generator();
	long edgesToGenerate = blockInfo.getElementCount();

	while (edgesToGenerate > 0) {
		long x = 0;
		long y = 0;

		// matrix constants are reset for each edge
		float a = this.a;
		float b = this.b;
		float c = this.c;
		float d = this.d;

		for (int bit = 0; bit < scale; bit++) {
			// generated next bit for source and target
			x <<= 1;
			y <<= 1;

			float random = rng.nextFloat();

			if (random <= a) {
			} else if (random <= a + b) {
				y += 1;
			} else if (random <= a + b + c) {
				x += 1;
			} else {
				x += 1;
				y += 1;
			}

			if (noiseEnabled) {
				// noise is bounded such that all parameters remain non-negative
				a *= 1.0 - noise / 2 + rng.nextFloat() * noise;
				b *= 1.0 - noise / 2 + rng.nextFloat() * noise;
				c *= 1.0 - noise / 2 + rng.nextFloat() * noise;
				d *= 1.0 - noise / 2 + rng.nextFloat() * noise;

				// normalize back to a + b + c + d = 1.0
				float norm = 1.0f / (a + b + c + d);

				a *= norm;
				b *= norm;
				c *= norm;

				// could multiply by norm, but subtract to minimize rounding error
				d = 1.0f - a - b - c;
			}
		}

		// if vertexCount is not a power-of-2 then discard edges outside the vertex range
		if (x < vertexCount && y < vertexCount) {
			source.setValue(x);
			target.setValue(y);

			out.collect(sourceToTarget);

			edgesToGenerate--;
		}
	}
}
 
Example 4
Source File: RMatGraph.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void flatMap(BlockInfo<T> blockInfo, Collector<Edge<LongValue, NullValue>> out)
		throws Exception {
	RandomGenerator rng = blockInfo.getRandomGenerable().generator();
	long edgesToGenerate = blockInfo.getElementCount();

	while (edgesToGenerate > 0) {
		long x = 0;
		long y = 0;

		// matrix constants are reset for each edge
		float a = this.a;
		float b = this.b;
		float c = this.c;
		float d = this.d;

		for (int bit = 0; bit < scale; bit++) {
			// generated next bit for source and target
			x <<= 1;
			y <<= 1;

			float random = rng.nextFloat();

			if (random <= a) {
			} else if (random <= a + b) {
				y += 1;
			} else if (random <= a + b + c) {
				x += 1;
			} else {
				x += 1;
				y += 1;
			}

			if (noiseEnabled) {
				// noise is bounded such that all parameters remain non-negative
				a *= 1.0 - noise / 2 + rng.nextFloat() * noise;
				b *= 1.0 - noise / 2 + rng.nextFloat() * noise;
				c *= 1.0 - noise / 2 + rng.nextFloat() * noise;
				d *= 1.0 - noise / 2 + rng.nextFloat() * noise;

				// normalize back to a + b + c + d = 1.0
				float norm = 1.0f / (a + b + c + d);

				a *= norm;
				b *= norm;
				c *= norm;

				// could multiply by norm, but subtract to minimize rounding error
				d = 1.0f - a - b - c;
			}
		}

		// if vertexCount is not a power-of-2 then discard edges outside the vertex range
		if (x < vertexCount && y < vertexCount) {
			source.setValue(x);
			target.setValue(y);

			out.collect(sourceToTarget);

			edgesToGenerate--;
		}
	}
}
 
Example 5
Source File: RMatGraph.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void flatMap(BlockInfo<T> blockInfo, Collector<Edge<LongValue, NullValue>> out)
		throws Exception {
	RandomGenerator rng = blockInfo.getRandomGenerable().generator();
	long edgesToGenerate = blockInfo.getElementCount();

	while (edgesToGenerate > 0) {
		long x = 0;
		long y = 0;

		// matrix constants are reset for each edge
		float a = this.a;
		float b = this.b;
		float c = this.c;
		float d = this.d;

		for (int bit = 0; bit < scale; bit++) {
			// generated next bit for source and target
			x <<= 1;
			y <<= 1;

			float random = rng.nextFloat();

			if (random <= a) {
			} else if (random <= a + b) {
				y += 1;
			} else if (random <= a + b + c) {
				x += 1;
			} else {
				x += 1;
				y += 1;
			}

			if (noiseEnabled) {
				// noise is bounded such that all parameters remain non-negative
				a *= 1.0 - noise / 2 + rng.nextFloat() * noise;
				b *= 1.0 - noise / 2 + rng.nextFloat() * noise;
				c *= 1.0 - noise / 2 + rng.nextFloat() * noise;
				d *= 1.0 - noise / 2 + rng.nextFloat() * noise;

				// normalize back to a + b + c + d = 1.0
				float norm = 1.0f / (a + b + c + d);

				a *= norm;
				b *= norm;
				c *= norm;

				// could multiply by norm, but subtract to minimize rounding error
				d = 1.0f - a - b - c;
			}
		}

		// if vertexCount is not a power-of-2 then discard edges outside the vertex range
		if (x < vertexCount && y < vertexCount) {
			source.setValue(x);
			target.setValue(y);

			out.collect(sourceToTarget);

			edgesToGenerate--;
		}
	}
}