Example usage for java.util Random nextFloat

List of usage examples for java.util Random nextFloat

Introduction

In this page you can find the example usage for java.util Random nextFloat.

Prototype

public float nextFloat() 

Source Link

Document

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

Usage

From source file:com.linkedin.pinot.common.utils.DataTableBuilderTest.java

@Test
public void testSimple() throws Exception {
    final DataType[] columnTypes = DataType.values();
    final String[] columnNames = new String[columnTypes.length];

    for (int i = 0; i < columnTypes.length; i++) {
        columnNames[i] = columnTypes[i].toString();
    }/*  ww  w  .  j  av  a 2s  . c  om*/
    final DataSchema schema = new DataSchema(columnNames, columnTypes);

    final DataTableBuilder builder = new DataTableBuilder(schema);
    builder.open();
    final Random r = new Random();
    final int NUM_ROWS = 100;

    final boolean[] boolArr = new boolean[NUM_ROWS];
    final char[] cArr = new char[NUM_ROWS];
    final byte[] bArr = new byte[NUM_ROWS];
    final short[] sArr = new short[NUM_ROWS];
    final int[] iArr = new int[NUM_ROWS];
    final float[] fArr = new float[NUM_ROWS];
    final long[] lArr = new long[NUM_ROWS];
    final double[] dArr = new double[NUM_ROWS];
    final String[] strArr = new String[NUM_ROWS];
    final Object[] oArr = new Object[NUM_ROWS];

    for (int rowId = 0; rowId < NUM_ROWS; rowId++) {
        builder.startRow();
        for (int colId = 0; colId < schema.columnNames.length; colId++) {
            final DataType type = columnTypes[colId];
            switch (type) {
            case BOOLEAN:
                final boolean bool = r.nextBoolean();
                boolArr[rowId] = bool;
                builder.setColumn(colId, bool);
                break;
            case CHAR:
                final char ch = (char) (r.nextInt(26) + 'a');
                cArr[rowId] = ch;
                builder.setColumn(colId, ch);
                break;
            case BYTE:
                final byte b = (byte) (r.nextInt((int) Math.pow(2, 8)));
                bArr[rowId] = b;
                builder.setColumn(colId, b);

                break;
            case SHORT:
                final short s = (short) (r.nextInt((int) Math.pow(2, 16)));
                sArr[rowId] = s;
                builder.setColumn(colId, s);

                break;
            case INT:
                final int i = (r.nextInt());
                iArr[rowId] = i;
                builder.setColumn(colId, i);

                break;
            case LONG:
                final long l = (r.nextLong());
                lArr[rowId] = l;
                builder.setColumn(colId, l);

                break;
            case FLOAT:
                final float f = (r.nextFloat());
                fArr[rowId] = f;
                builder.setColumn(colId, f);
                break;
            case DOUBLE:
                final double d = (r.nextDouble());
                dArr[rowId] = d;
                builder.setColumn(colId, d);
                break;
            case STRING:
                final String str = new BigInteger(130, r).toString(32);
                strArr[rowId] = str;
                builder.setColumn(colId, str);
                break;
            case OBJECT:
                final A obj = new A(r.nextInt());
                oArr[rowId] = obj;
                builder.setColumn(colId, obj);

                break;
            default:
                break;
            }
        }
        builder.finishRow();
    }
    builder.seal();
    final DataTable dataTable = builder.build();
    //System.out.println(dataTable);
    validate(dataTable, NUM_ROWS, schema, boolArr, cArr, bArr, sArr, iArr, fArr, lArr, dArr, strArr, oArr);
    final byte[] bytes = dataTable.toBytes();

    final DataTable newDataTable = new DataTable(bytes);
    validate(newDataTable, NUM_ROWS, schema, boolArr, cArr, bArr, sArr, iArr, fArr, lArr, dArr, strArr, oArr);

}

From source file:org.nuxeo.ecm.core.storage.sql.TestSQLBackend.java

@Test
public void testMoveMany() throws Exception {
    Session session = repository.getConnection();
    Node root = session.getRootNode();
    ArrayList<Node> nodes = new ArrayList<Node>();
    nodes.add(root);/*w  ww .  j  a  va2 s  .  c  o  m*/
    Random rnd = new Random(123456);
    List<String[]> graph = new ArrayList<String[]>();
    for (int i = 0; i < 200; i++) {
        // create a node under a random node
        Node parent = nodes.get((int) Math.floor(rnd.nextFloat() * nodes.size()));
        Node child = session.addChildNode(parent, "child" + i, null, "TestDoc", false);
        nodes.add(child);
        // update graph
        addEdge(graph, parent.getId().toString(), child.getId().toString());
        if ((i % 5) == 0) {
            // move a random node under a random parent
            int ip, ic;
            Node p, c;
            String pid, cid;
            do {
                ip = (int) Math.floor(rnd.nextFloat() * nodes.size());
                ic = (int) Math.floor(rnd.nextFloat() * nodes.size());
                p = nodes.get(ip);
                c = nodes.get(ic);
                pid = p.getId().toString();
                cid = c.getId().toString();
                if (isUnder(graph, cid, pid)) {
                    // check we have an error for this move
                    try {
                        session.move(c, p, c.getName());
                        fail("shouldn't be able to move");
                    } catch (Exception e) {
                        // ok
                    }
                    ic = 0; // try again
                }
            } while (ic == 0 || ip == ic);
            String oldpid = c.getParentId().toString();
            session.move(c, p, c.getName());
            removeEdge(graph, oldpid, cid);
            addEdge(graph, pid, cid);
        }
    }
    session.save();

    // dumpGraph(graph);
    // dumpDescendants(buildDescendants(graph, root.getId().toString()));
}

From source file:es.sm2.openppm.front.servlets.ProjectControlServlet.java

/**
  * Chart kpis//w  w w  . java2s . com
  * 
  * @param req
  * @param resp
  * @throws IOException 
  */
private void kpiChartJX(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    int idProject = ParamUtil.getInteger(req, "id");

    PrintWriter out = resp.getWriter();

    try {
        Date minDate = null;
        Date maxDate = null;
        SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy");
        SimpleDateFormat tempDate = new SimpleDateFormat("MM/dd/yyyy");
        Random random = new Random();
        Float r = null;
        Float g = null;
        Float b = null;
        String[] defaultColors = new String[] { "#4bb2c5", "#c5b47f", "#EAA228", "#579575", "#839557",
                "#958c12", "#953579", "#4b5de4", "#d8b83f", "#ff5800", "#0085cc" };
        Boolean insufficientDates = true;

        HistorickpiLogic historickpiLogic = new HistorickpiLogic();
        ProjectLogic projectLogic = new ProjectLogic(getSettings(req), getResourceBundle(req));

        JSONArray kpisValuesJSON = new JSONArray();
        JSONArray kpisNamesJSON = new JSONArray();
        JSONArray kpisColorsJSON = new JSONArray();
        JSONObject updateJSON = new JSONObject();

        List<String> joins = new ArrayList<String>();
        joins.add(Projectkpi.METRICKPI);
        Project project = projectLogic.findById(idProject);

        ProjectKpiLogic projectKpiLogic = new ProjectKpiLogic();
        List<Projectkpi> projectKpis = projectKpiLogic.findByRelation(Projectkpi.PROJECT, project, joins);

        //colors
        for (String defaultColor : defaultColors) {
            kpisColorsJSON.add(defaultColor);
        }

        int varianceColors = projectKpis.size() > defaultColors.length
                ? projectKpis.size() - defaultColors.length
                : 0;

        for (int i = 0; i < varianceColors; i++) {
            random.setSeed(new Date().getTime());
            r = random.nextFloat();
            g = random.nextFloat();
            b = random.nextFloat();
            Color randomColor = new Color(r, g, b);
            String rgb = Integer.toHexString(randomColor.getRGB());
            rgb = rgb.substring(2, rgb.length());
            rgb = StringPool.POUND.concat(rgb);

            kpisColorsJSON.add(rgb);
        }

        List<String> joinsHistoricKpi = new ArrayList<String>();
        joinsHistoricKpi.add(Historickpi.EMPLOYEE);
        joinsHistoricKpi.add(Historickpi.EMPLOYEE + "." + Employee.CONTACT);

        for (Projectkpi kpi : projectKpis) {
            JSONArray kpiJSON = new JSONArray();

            List<Historickpi> historic = historickpiLogic.findByRelation(Historickpi.PROJECTKPI,
                    new Projectkpi(kpi.getIdProjectKpi()), Historickpi.ACTUALDATE, Constants.ASCENDENT,
                    joinsHistoricKpi);

            if (!historic.isEmpty()) {

                String tempDateStr = tempDate.format(historic.get(0).getActualDate());

                for (Historickpi hKpi : historic) {
                    JSONArray hKpiJSON = new JSONArray();

                    hKpiJSON.add(date.format(hKpi.getActualDate()));
                    hKpiJSON.add(hKpi.getValueKpi() == null ? 0 : hKpi.getValueKpi());

                    if (!tempDateStr.equals(date.format(hKpi.getActualDate()))) {
                        insufficientDates = false;
                    }

                    kpiJSON.add(hKpiJSON);
                }
            }
            kpisValuesJSON.add(kpiJSON);

            if (kpi.getMetrickpi() != null) {
                kpisNamesJSON.add(kpi.getMetrickpi().getName());
            } else {
                kpisNamesJSON.add(kpi.getSpecificKpi());
            }

            /* min and max dates*/
            if (!insufficientDates) {
                Date tempMinDate = historic.get(0).getActualDate();
                Date tempMaxDate = historic.get(historic.size() - 1).getActualDate();

                if (minDate == null && maxDate == null) {
                    minDate = tempMinDate;
                    maxDate = tempMaxDate;
                } else {
                    if (tempMinDate.compareTo(minDate) < 0) {
                        minDate = tempMinDate;
                    }
                    if (tempMaxDate.compareTo(maxDate) > 0) {
                        maxDate = tempMaxDate;
                    }
                }
            }

        }

        ChartJQPLOT chart = new ChartJQPLOT(minDate, maxDate);
        updateJSON.put("tickInterval", chart.getTickInterval());
        updateJSON.put("minDate", chart.addDate(minDate, -1));
        updateJSON.put("maxDate", chart.addDate(maxDate, 1));

        updateJSON.put("kpisColors", kpisColorsJSON);
        updateJSON.put("kpisNames", kpisNamesJSON);
        updateJSON.put("kpisValues", kpisValuesJSON);
        updateJSON.put("insufficientDates", insufficientDates);

        out.print(updateJSON);
    } catch (Exception e) {
        ExceptionUtil.evalueExceptionJX(out, req, getResourceBundle(req), LOGGER, e);
    } finally {
        out.close();
    }
}