List of usage examples for java.util Random nextDouble
public double nextDouble()
From source file:org.apache.jetspeed.security.mfa.impl.CaptchaImageResource.java
public void init() { boolean emptyBackground = true; if (config.isUseImageBackground() && background != null) { ByteArrayInputStream is = new ByteArrayInputStream(background); JPEGImgDecoder decoder = new DefaultJPEGImgDecoder(); try {/*w ww.j a va 2s .c o m*/ this.image = decoder.decodeAsBufferedImage(is); this.width = image.getWidth(); this.height = image.getHeight(); emptyBackground = false; } catch (Exception e) { emptyBackground = true; } } if (emptyBackground) { this.width = config.getTextMarginLeft() * 2; this.height = config.getTextMarginBottom() * 6; } char[] chars = challengeId.toCharArray(); charAttsList = new ArrayList(); TextLayout text = null; AffineTransform textAt = null; String[] fontNames = config.getFontNames(); for (int i = 0; i < chars.length; i++) { // font name String fontName = (fontNames.length == 1) ? fontNames[0] : fontNames[randomInt(0, fontNames.length)]; // rise int rise = config.getTextRiseRange(); if (rise > 0) { rise = randomInt(config.getTextMarginBottom(), config.getTextMarginBottom() + config.getTextRiseRange()); } if (config.getTextShear() > 0.0 || config.getTextRotation() > 0) { // rotation double dRotation = 0.0; if (config.getTextRotation() > 0) { dRotation = Math.toRadians(randomInt(-(config.getTextRotation()), config.getTextRotation())); } // shear double shearX = 0.0; double shearY = 0.0; if (config.getTextShear() > 0.0) { Random ran = new Random(); shearX = ran.nextDouble() * config.getTextShear(); shearY = ran.nextDouble() * config.getTextShear(); } CharAttributes cf = new CharAttributes(chars[i], fontName, dRotation, rise, shearX, shearY); charAttsList.add(cf); text = new TextLayout(chars[i] + "", getFont(fontName), new FontRenderContext(null, config.isFontAntialiasing(), false)); textAt = new AffineTransform(); if (config.getTextRotation() > 0) textAt.rotate(dRotation); if (config.getTextShear() > 0.0) textAt.shear(shearX, shearY); } else { CharAttributes cf = new CharAttributes(chars[i], fontName, 0, rise, 0.0, 0.0); charAttsList.add(cf); } if (emptyBackground) { Shape shape = text.getOutline(textAt); // this.width += text.getBounds().getWidth(); this.width += (int) shape.getBounds2D().getWidth(); this.width += config.getTextSpacing() + 1; if (this.height < (int) shape.getBounds2D().getHeight() + rise) { this.height = (int) shape.getBounds2D().getHeight() + rise; } } } if (emptyBackground) { this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D gfx = (Graphics2D) this.image.getGraphics(); gfx.setBackground(Color.WHITE); gfx.clearRect(0, 0, width, height); } }
From source file:org.jtransforms.utils.IOUtils.java
/** * Fills 2D matrix with random numbers.// w w w .j a v a 2s .co m * @param n1 rows * @param n2 columns * @param m 2D matrix */ public static void fillMatrix_2D(long n1, long n2, double[] m) { Random r = new Random(2); for (int i = 0; i < n1; i++) { for (int j = 0; j < n2; j++) { m[(int) (i * n2 + j)] = r.nextDouble(); } } }
From source file:org.jtransforms.utils.IOUtils.java
/** * Fills 2D matrix with random numbers.//ww w. j a va 2 s.c om * @param n1 rows * @param n2 columns * @param m 2D matrix */ public static void fillMatrix_2D(long n1, long n2, DoubleLargeArray m) { Random r = new Random(2); for (long i = 0; i < n1; i++) { for (long j = 0; j < n2; j++) { m.setDouble(i * n2 + j, r.nextDouble()); } } }
From source file:tml.vectorspace.factorisation.MultiDimensionalScalingNR.java
public Instances scale(Instances instances) { // approximation error error = 0.0;// w w w . ja va 2 s . c o m double error_previous; // number of points int n = instances.numInstances(); // distance between points in the p-dimensional layout d = new Matrix(n, n); Matrix d_previous; // dissimilarity between vectors d_hat = new Matrix(n, n); // points instances FastVector attributes = new FastVector(p); attributes.addElement(new Attribute("X")); attributes.addElement(new Attribute("Y")); Instances x = new Instances("MDS", attributes, instances.numInstances()); Instances x_previous; // initialise points sequence ArrayList<Integer> kseq = new ArrayList<Integer>(); for (int k = 0; k < n; k++) { kseq.add(k); } // initialise x if (initialX != null) { x = new Instances(initialX); } else { Random rand = new Random(); for (int k = 0; k < n; k++) { Instance x_inst = new Instance(p); x_inst.setValue(X, rand.nextDouble() - rand.nextInt(1)); x_inst.setValue(Y, rand.nextDouble() - rand.nextInt(1)); x.add(x_inst); } } // calculate d for (int j = 0; j < n; j++) { for (int i = 0; i < j; i++) { double distance = this.distance(x.instance(i), x.instance(j)); d.set(i, j, distance); d.set(j, i, distance); double dissimilarity = this.dissimilarity(instances.instance(i), instances.instance(j)); d_hat.set(i, j, dissimilarity); d_hat.set(j, i, dissimilarity); if (d_hat.get(i, j) != 0) { error += Math.pow(d.get(i, j) - d_hat.get(i, j), 2) / Math.pow(d_hat.get(i, j), 2); } } } // record previous results error_previous = error; d_previous = d.copy(); x_previous = new Instances(x); // start of Newton-Raphson method logger.info("Starting Newton-Raphson MDS."); for (int iter = 0; iter < maxIterations; iter++) { // randomise points sequence to ensure faster convergence Collections.shuffle(kseq); for (int k : kseq) { Matrix gradient = new Matrix(p, 1); Matrix hessian = new Matrix(p, p); // calculate gradient vector for (int a = 0; a < p; a++) { double sum = 0; for (int l = 0; l < n; l++) { if (k != l) { if (d.get(k, l) != 0 && d_hat.get(k, l) != 0) { sum += ((d.get(k, l) - d_hat.get(k, l)) / (d.get(k, l) * Math.pow(d_hat.get(k, l), 2))) * (x.instance(k).value(a) - x.instance(l).value(a)); } } } gradient.set(a, 0, 2 * sum); } // calculate hessian matrix for (int a = 0; a < p; a++) { for (int b = 0; b < p; b++) { double sum = 0.0; if (a != b) { for (int l = 0; l < n; l++) { if (k != l) { if (d.get(k, l) != 0 && d_hat.get(k, l) != 0) { sum += ((x.instance(k).value(a) - x.instance(l).value(a)) * (x.instance(k).value(b) - x.instance(l).value(b))) / (Math.pow(d.get(k, l), 3) * d_hat.get(k, l)); } } } sum = 2 * sum; } else { for (int l = 0; l < n; l++) { if (k != l) { if (d_hat.get(k, l) != 0 && d.get(k, l) != 0) { sum += (1.0 / Math.pow(d_hat.get(k, l), 2)) - (Math.pow(d.get(k, l), 2) - Math.pow((x.instance(k).value(a) - x.instance(l).value(a)), 2)) / (Math.pow(d.get(k, l), 3) * d_hat.get(k, l)); } } } sum = 2 * sum; } hessian.set(a, b, sum); } } // update x Matrix x_k = new Matrix(x.instance(k).toDoubleArray(), p); Matrix x_k_tilda = x_k.minus(hessian.inverse().times(gradient)); x.instance(k).setValue(X, x_k_tilda.get(X, 0)); x.instance(k).setValue(Y, x_k_tilda.get(Y, 0)); } // calculate d and error error = 0; for (int j = 0; j < n; j++) { for (int i = 0; i < j; i++) { double distance = this.distance(x.instance(i), x.instance(j)); d.set(i, j, distance); d.set(j, i, distance); if (d_hat.get(i, j) != 0) { error += Math.pow(d.get(i, j) - d_hat.get(i, j), 2) / Math.pow(d_hat.get(i, j), 2); } } } if (error < error_previous) { logger.debug(iter + ".\t error " + error); if (error_previous - error <= tolerence) { break; } error_previous = error; d_previous = d.copy(); x_previous = new Instances(x); } else // invalidates last run { x = new Instances(x_previous); d = d_previous.copy(); } } logger.info("Finished Newton-Raphson MDS."); return x; }
From source file:org.apache.hadoop.hive.ql.exec.vector.VectorRandomRowSource.java
public static Object randomObject(int column, Random r, PrimitiveCategory[] primitiveCategories, PrimitiveTypeInfo[] primitiveTypeInfos, String[] alphabets, boolean addEscapables, String needsEscapeStr) {//from ww w .j a va 2 s . co m PrimitiveCategory primitiveCategory = primitiveCategories[column]; PrimitiveTypeInfo primitiveTypeInfo = primitiveTypeInfos[column]; try { switch (primitiveCategory) { case BOOLEAN: return Boolean.valueOf(r.nextInt(1) == 1); case BYTE: return Byte.valueOf((byte) r.nextInt()); case SHORT: return Short.valueOf((short) r.nextInt()); case INT: return Integer.valueOf(r.nextInt()); case LONG: return Long.valueOf(r.nextLong()); case DATE: return RandomTypeUtil.getRandDate(r); case FLOAT: return Float.valueOf(r.nextFloat() * 10 - 5); case DOUBLE: return Double.valueOf(r.nextDouble() * 10 - 5); case STRING: case CHAR: case VARCHAR: { String result; if (alphabets != null && alphabets[column] != null) { result = RandomTypeUtil.getRandString(r, alphabets[column], r.nextInt(10)); } else { result = RandomTypeUtil.getRandString(r); } if (addEscapables && result.length() > 0) { int escapeCount = 1 + r.nextInt(2); for (int i = 0; i < escapeCount; i++) { int index = r.nextInt(result.length()); String begin = result.substring(0, index); String end = result.substring(index); Character needsEscapeChar = needsEscapeStr.charAt(r.nextInt(needsEscapeStr.length())); result = begin + needsEscapeChar + end; } } switch (primitiveCategory) { case STRING: return result; case CHAR: return new HiveChar(result, ((CharTypeInfo) primitiveTypeInfo).getLength()); case VARCHAR: return new HiveChar(result, ((VarcharTypeInfo) primitiveTypeInfo).getLength()); default: throw new Error("Unknown primitive category " + primitiveCategory); } } case BINARY: return getRandBinary(r, 1 + r.nextInt(100)); case TIMESTAMP: return RandomTypeUtil.getRandTimestamp(r); case INTERVAL_YEAR_MONTH: return getRandIntervalYearMonth(r); case INTERVAL_DAY_TIME: return getRandIntervalDayTime(r); case DECIMAL: return getRandHiveDecimal(r, (DecimalTypeInfo) primitiveTypeInfo); default: throw new Error("Unknown primitive category " + primitiveCategory); } } catch (Exception e) { throw new RuntimeException("randomObject failed on column " + column + " type " + primitiveCategory, e); } }
From source file:edu.stanford.muse.util.EmailUtils.java
private static Map<String, String> sample(Map<String, String> full, double p) { Random rand = new Random(); Map<String, String> sample = new LinkedHashMap<>(); for (String e : full.keySet()) { if (rand.nextDouble() < p) sample.put(e, full.get(e));/*from w w w.j a v a 2 s .com*/ } return sample; }
From source file:RosAdProgram.model.Model.java
public int mask(double probability, int data) { int shiftVariable = 1; int tempMask = 0; int tempBitError = 0; // ? Random randomNumberGenerator = new Random(); for (int i = 0; i != 8; shiftVariable = shiftVariable << 1, i++) { //System.out.println(" "+i); if (randomNumberGenerator.nextDouble() > probability) { if (i == 0) { //? ? ? //System.out.format("\n i = %d ? ? %h ",i, tempMask ); tempMask = tempMask | shiftVariable; // ? ? ? ? tempBitError++; // ? ? // System.out.format(" %h . %h\n", shiftVariable,tempMask ); } else { // ? //System.out.format("i = %d ? ? %h ",i, tempMask ); tempMask = tempMask | shiftVariable; tempBitError++;//from ww w . ja va 2 s . co m //System.out.format(" %h . %h\n", shiftVariable,tempMask ); } } else { //System.out.println(" ("+i+")"); } } setBitError(tempBitError); // ? ? if (tempBitError != 0) { // ? , ? . setBlockErrorFlag(true); //? . ??? ? . setByteError(1); // ? ? } // System.out.format("\n\n\n ? ? = %h\n", tempMask ); // System.out.format("\n\n\n = %h\n", data); // System.out.format("\n\n\n ? = %h\n", data ^ tempMask); // System.out.println("-----------------------------------------------"); return data ^ tempMask; }
From source file:br.fapesp.myutils.MyUtils.java
public static double[] uniformlySample(double low, double high, int nsamples, Random rng, long seed) { double[] samples = new double[nsamples]; Random r; if (rng == null) r = new Random(seed); else/*ww w .ja v a 2 s . com*/ r = rng; for (int i = 0; i < nsamples; i++) samples[i] = r.nextDouble() * (high - low) + low; return samples; }
From source file:edu.cornell.med.icb.goby.modes.ReformatCompactReadsMode.java
/** * @param lo lower limit of range/* www .j a v a2s . c o m*/ * @param hi upper limit of range * @return a random integer in the range <STRONG>lo</STRONG>, * <STRONG>lo</STRONG>+1, ... ,<STRONG>hi</STRONG> */ private int chooseRandom(final Random random, final int lo, final int hi) { final double r = random.nextDouble(); final int result = (int) ((long) lo + (long) ((1L + (long) hi - (long) lo) * r)); assert result >= lo && result <= hi; return result; }
From source file:org.jtransforms.utils.IOUtils.java
/** * Fills 3D matrix with random numbers.//from w w w.j a va 2s .co m * @param n1 slices * @param n2 rows * @param n3 columns * @param m 3D matrix */ public static void fillMatrix_3D(long n1, long n2, long n3, double[][][] m) { Random r = new Random(2); for (int i = 0; i < n1; i++) { for (int j = 0; j < n2; j++) { for (int k = 0; k < n3; k++) { m[i][j][k] = r.nextDouble(); } } } }