Example usage for org.apache.commons.math3.random RandomDataGenerator nextLong

List of usage examples for org.apache.commons.math3.random RandomDataGenerator nextLong

Introduction

In this page you can find the example usage for org.apache.commons.math3.random RandomDataGenerator nextLong.

Prototype

public long nextLong(long lower, long upper) throws NumberIsTooLargeException 

Source Link

Usage

From source file:ke.co.tawi.babblesms.server.utils.randomgenerate.IncomingLogGenerator.java

/**
 * @param args//from ww w .  j  a  v  a2 s .c  o m
 */
public static void main(String[] args) {
    System.out.println("Have started IncomingSMSGenerator.");
    File outFile = new File("/tmp/logs/incomingSMS.csv");
    RandomDataGenerator randomDataImpl = new RandomDataGenerator();

    String randomStrFile = "/tmp/random.txt";

    List<String> randomStrings = new ArrayList<>();
    int randStrLength = 0;

    long startDate = 1412380800; // Unix time in seconds for Oct 4 2014
    long stopDate = 1420070340; // Unix time for in seconds Dec 31 2014

    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 2011-06-01 00:16:45" 

    List<String> shortcodeUuids = new ArrayList<>();
    shortcodeUuids.add("094def52-bc18-4a9e-9b84-c34cc6476c75");
    shortcodeUuids.add("e9570c5d-0cc4-41e5-81df-b0674e9dda1e");
    shortcodeUuids.add("a118c8ea-f831-4288-986d-35e22c91fc4d");
    shortcodeUuids.add("a2688d72-291b-470c-8926-31a903f5ed0c");
    shortcodeUuids.add("9bef62f6-e682-4efd-98e9-ca41fa4ef993");

    int shortcodeCount = shortcodeUuids.size() - 1;

    try {
        randomStrings = FileUtils.readLines(new File(randomStrFile));
        randStrLength = randomStrings.size();

        for (int j = 0; j < 30000; j++) {
            FileUtils.write(outFile,
                    // shortcodeUuids.get(randomDataImpl.nextInt(0, shortcodeCount)) + "|"   // Destination 
                    UUID.randomUUID().toString() + "|" // Unique Code
                            + randomDataImpl.nextLong(new Long("254700000000").longValue(),
                                    new Long("254734999999").longValue())
                            + "|" // Origin
                            + shortcodeUuids.get(randomDataImpl.nextInt(0, shortcodeCount)) + "|"
                            + randomStrings.get(randomDataImpl.nextInt(0, randStrLength - 1)) + "|" // Message
                            + "N|" // deleted
                            + dateFormatter.format(
                                    new Date(randomDataImpl.nextLong(startDate, stopDate) * 1000))
                            + "\n", // smsTime                  
                    true); // Append to file                              
        }

    } catch (IOException e) {
        System.err.println("IOException in main.");
        e.printStackTrace();
    }

    System.out.println("Have finished IncomingSMSGenerator.");
}

From source file:edu.cmu.tetrad.sem.LargeSemSimulator.java

public BoxDataSet simulateDataAcyclicConcurrent(int sampleSize) {
    int numVars = variableNodes.size();
    setupModel(numVars);//  w w  w.j  a  va  2  s . c o m

    System.out.println("Tier ordering");

    final int[][] _parents = parents;
    final double[][] _coefs = coefs;

    //        final double[][] _data = new double[sampleSize][numVars];
    final double[][] _data = new double[numVars][sampleSize];

    System.out.println("Starting simulation task");

    // This random number generator is not thread safe, so we make a new one each time.
    RandomGenerator apacheGen = new Well19937a(new Date().getTime());
    final RandomDataGenerator generator = new RandomDataGenerator(new SynchronizedRandomGenerator(apacheGen));

    //Do the simulation.
    class SimulationTask extends RecursiveTask<Boolean> {
        private int chunk;
        private int from;
        private int to;

        public SimulationTask(int chunk, int from, int to) {
            this.chunk = chunk;
            this.from = from;
            this.to = to;
        }

        @Override
        protected Boolean compute() {
            RandomGenerator apacheGen = new Well44497b(generator.nextLong(0, Long.MAX_VALUE));
            RandomDataGenerator generatorLocal = new RandomDataGenerator(apacheGen);

            if (to - from <= chunk) {
                for (int row = from; row < to; row++) {
                    if (row % 100 == 0)
                        out.println("Row " + row);

                    for (int i = 0; i < tierIndices.length; i++) {
                        int col = tierIndices[i];

                        double value = generatorLocal.nextGaussian(0, sqrt(errorVars[col]));

                        for (int j = 0; j < _parents[col].length; j++) {
                            int parent = _parents[col][j];
                            final double coef = _coefs[col][j];
                            final double v = _data[parent][row];
                            value += v * coef;

                            if (Double.isNaN(value)) {
                                throw new IllegalArgumentException();
                            }
                        }

                        value += means[col];

                        _data[col][row] = value;
                    }
                }

                return true;
            } else {
                List<SimulationTask> simulationTasks = new ArrayList<SimulationTask>();

                int mid = (to - from) / 2;

                simulationTasks.add(new SimulationTask(chunk, from, from + mid));
                simulationTasks.add(new SimulationTask(chunk, from + mid, to));

                invokeAll(simulationTasks);

                return true;
            }
        }
    }

    int chunk = 25;

    pool.invoke(new SimulationTask(chunk, 0, sampleSize));

    return new BoxDataSet(new VerticalDoubleDataBox(_data), variableNodes);
    //        return ColtDataSet.makeContinuousData(variableNodes, _data);
}

From source file:ke.co.tawi.babblesms.server.persistence.utils.DateResetUtil.java

/**
 * Reset dates for Incoming SMS.// www .  j a v a  2 s.c om
 * 
 * @param startDate A start date in the format yyyy-MM-dd HH:mm:ss
 * @param endDate An end date in the format yyyy-MM-dd HH:mm:ss
 */
public void resetIncomingDates(String startDate, String endDate) {

    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Example 2011-06-01 00:16:45" 
    List<String> uuids = new ArrayList<>();

    RandomDataGenerator generator = new RandomDataGenerator();

    // Get UUIDs of Incoming SMS are in the database
    try (Connection conn = dbCredentials.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("SELECT uuid FROM incominglog;");) {
        ResultSet rset = pstmt.executeQuery();

        while (rset.next()) {
            uuids.add(rset.getString("uuid"));
        }

    } catch (SQLException e) {
        logger.error("SQLException when getting uuids of incominglog.");
        logger.error(ExceptionUtils.getStackTrace(e));
    }

    try (Connection conn = dbCredentials.getConnection();
            PreparedStatement pstmt = conn
                    .prepareStatement("UPDATE incominglog SET logTime=? " + "WHERE Uuid=?;");) {
        long start = dateFormatter.parse(startDate).getTime();
        long end = dateFormatter.parse(endDate).getTime();

        for (String uuid : uuids) {
            pstmt.setTimestamp(1, new Timestamp(generator.nextLong(start, end)));
            pstmt.setString(2, uuid);

            pstmt.executeUpdate();
        }

    } catch (SQLException e) {
        logger.error("SQLException when trying to reset dates of incomingLog.");
        logger.error(ExceptionUtils.getStackTrace(e));

    } catch (ParseException e) {
        logger.error("ParseException when trying to reset dates of incomingLog.");
        logger.error(ExceptionUtils.getStackTrace(e));
    }
}

From source file:ke.co.tawi.babblesms.server.persistence.utils.DateResetUtil.java

/**
 * Reset dates for Outgoing SMS./*w w  w  .  j av  a 2  s  . c  o  m*/
 * 
 * @param startDate A start date in the format yyyy-MM-dd HH:mm:ss
 * @param endDate An end date in the format yyyy-MM-dd HH:mm:ss
 */
public void resetOutgoingDates(String startDate, String endDate) {

    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Example 2011-06-01 00:16:45" 
    List<String> uuids = new ArrayList<>();

    RandomDataGenerator generator = new RandomDataGenerator();

    // Get UUIDs of Incoming SMS are in the database
    try (Connection conn = dbCredentials.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("SELECT uuid FROM outgoinglog;");) {
        ResultSet rset = pstmt.executeQuery();

        while (rset.next()) {
            uuids.add(rset.getString("uuid"));
        }

    } catch (SQLException e) {
        logger.error("SQLException when getting uuids of outgoinglog.");
        logger.error(ExceptionUtils.getStackTrace(e));
    }

    try (Connection conn = dbCredentials.getConnection();
            PreparedStatement pstmt = conn
                    .prepareStatement("UPDATE outgoinglog SET logTime=? " + "WHERE Uuid=?;");) {
        long start = dateFormatter.parse(startDate).getTime();
        long end = dateFormatter.parse(endDate).getTime();

        for (String uuid : uuids) {
            pstmt.setTimestamp(1, new Timestamp(generator.nextLong(start, end)));
            pstmt.setString(2, uuid);

            pstmt.executeUpdate();
        }

    } catch (SQLException e) {
        logger.error("SQLException when trying to reset dates of outgoinglog.");
        logger.error(ExceptionUtils.getStackTrace(e));

    } catch (ParseException e) {
        logger.error("ParseException when trying to reset dates of outgoinglog.");
        logger.error(ExceptionUtils.getStackTrace(e));
    }

}

From source file:org.cruk.seq.SampleFastq.java

/**
 * Samples records from a FASTQ file using reservoir sampling.
 *
 * @param fastqFilenames the FASTQ file(s).
 * @param sampleSize the number of records to sample.
 * @param maxRecordsToSample the maximum number of records to sample from.
 * @param removeDescriptions to remove sequence identifiers/descriptions to save on space.
 * @return the sampled FASTQ records.//from   ww  w .j a v a2  s . c  o  m
 * @throws IOException
 * @throws FastqFormatException
 */
private Fastq[] reservoirSampling(String[] fastqFilenames, int sampleSize, long maxSampleFrom,
        boolean removeDescriptions) throws IOException, FastqFormatException {
    FastqReader reader = new FastqReader(fastqFilenames, true);

    Fastq[] records = new Fastq[sampleSize];

    for (int i = 0; i < sampleSize; i++) {
        Fastq record = reader.readFastq();
        if (record == null)
            return Arrays.copyOf(records, i);
        if (removeDescriptions)
            record.setDescription(null);
        records[i] = record;
    }

    RandomDataGenerator rand = new RandomDataGenerator();

    for (long i = sampleSize; i < maxSampleFrom; i++) {
        Fastq record = reader.readFastq();
        if (record == null)
            break;

        long j = rand.nextLong(0l, i);
        if (j < sampleSize)
            records[(int) j] = record;
    }

    reader.close();

    return records;
}

From source file:play.sax.main.Main.java

private static TimeSeries generateRandomTimeSeries(final int noOfEntries) {

    final TimeSeries series = new TimeSeries();
    RandomDataGenerator randomData = new RandomDataGenerator();
    Random rand = new Random();

    for (int i = 0; i < noOfEntries; i++) {
        final Long time = randomData.nextLong(0, 20000);
        final Double value = DATA_LOWER_BOUND + DATA_RANGE * rand.nextDouble();
        final TimeSeriesEntry entry = new TimeSeriesEntry(time, value);

        series.addTimeSeriesEntry(entry);
    }/*from  w  w  w. jav  a 2  s.com*/
    return series;
}

From source file:uk.bl.wa.hadoop.mapred.ReservoirSamplingReducer.java

@Override
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter)
        throws IOException {

    Text item;/*w  w w. java2 s  .co  m*/
    long numItemsSeen = 0;
    Vector<Text> reservoir = new Vector<Text>();
    RandomDataGenerator random = new RandomDataGenerator();
    // Fix the seed so repoducible by default:
    random.reSeed(defaultSeed);

    // Iterate through all values:
    while (values.hasNext()) {
        item = values.next();

        if (reservoir.size() < numSamples) {
            // reservoir not yet full, just append
            reservoir.add(item);
        } else {
            // find a sample to replace
            long rIndex = random.nextLong(0, numItemsSeen);
            if (rIndex < numSamples) {
                reservoir.set((int) rIndex, item);
            }
        }
        numItemsSeen++;
    }

    // Choose the output:
    Text outKey = key;
    OutputCollector<Text, Text> collector;
    int pos = key.find("__");
    if (pos == -1) {
        collector = output;
    } else {
        String[] fp = key.toString().split("__");
        collector = getCollector(fp[0], fp[1], reporter);
        outKey = new Text(fp[1]);
    }

    // Now output the sample:
    for (Text sto : reservoir) {
        collector.collect(outKey, sto);
    }
}

From source file:uk.bl.wa.hadoop.mapreduce.ReservoirSamplingReducer.java

@Override
protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
        throws IOException, InterruptedException {

    long numItemsSeen = 0;
    Vector<Text> reservoir = new Vector<Text>();
    RandomDataGenerator random = new RandomDataGenerator();
    // Fix the seed so reproducible by default:
    random.reSeed(defaultSeed);//from   w  w  w .j  a  v a 2s.c  om

    // Iterate through all values:
    for (Text item : values) {
        // Fill the reservoir:
        if (reservoir.size() < numSamples) {
            // reservoir not yet full, just append
            reservoir.add(item);
        } else {
            // find a sample to replace
            long rIndex = random.nextLong(0, numItemsSeen);
            if (rIndex < numSamples) {
                reservoir.set((int) rIndex, item);
            }
        }
        numItemsSeen++;
    }

    // Now output the sample:
    for (Text sto : reservoir) {
        context.write(key, sto);
    }
}