Example usage for org.apache.commons.math.random RandomData nextSample

List of usage examples for org.apache.commons.math.random RandomData nextSample

Introduction

In this page you can find the example usage for org.apache.commons.math.random RandomData nextSample.

Prototype

Object[] nextSample(Collection<?> c, int k);

Source Link

Document

Returns an array of k objects selected randomly from the Collection c.

Usage

From source file:org.apache.accumulo.test.randomwalk.concurrent.Config.java

private void changeTableSetting(RandomData random, State state, Environment env, Properties props)
        throws Exception {
    // pick a random property
    int choice = random.nextInt(0, tableSettings.length - 1);
    Setting setting = tableSettings[choice];

    // pick a random table
    SortedSet<String> tables = env.getConnector().tableOperations().list().tailSet("ctt").headSet("ctu");
    if (tables.isEmpty())
        return;//w  ww .  j  a va 2s  .  c  o m
    String table = random.nextSample(tables, 1)[0].toString();

    // generate a random value
    long newValue = random.nextLong(setting.min, setting.max);
    state.set(LAST_TABLE_SETTING, table + "," + choice);
    log.debug("Setting " + setting.property.getKey() + " on table " + table + " to " + newValue);
    try {
        env.getConnector().tableOperations().setProperty(table, setting.property.getKey(), "" + newValue);
    } catch (AccumuloException ex) {
        if (ex.getCause() instanceof ThriftTableOperationException) {
            ThriftTableOperationException ttoe = (ThriftTableOperationException) ex.getCause();
            if (ttoe.type == TableOperationExceptionType.NOTFOUND)
                return;
        }
        throw ex;
    }
}

From source file:org.apache.accumulo.test.randomwalk.concurrent.Config.java

private void changeNamespaceSetting(RandomData random, State state, Environment env, Properties props)
        throws Exception {
    // pick a random property
    int choice = random.nextInt(0, tableSettings.length - 1);
    Setting setting = tableSettings[choice];

    // pick a random table
    SortedSet<String> namespaces = env.getConnector().namespaceOperations().list().tailSet("nspc")
            .headSet("nspd");
    if (namespaces.isEmpty())
        return;/*from  w ww  .j a  v  a2  s .c  o  m*/
    String namespace = random.nextSample(namespaces, 1)[0].toString();

    // generate a random value
    long newValue = random.nextLong(setting.min, setting.max);
    state.set(LAST_NAMESPACE_SETTING, namespace + "," + choice);
    log.debug("Setting " + setting.property.getKey() + " on namespace " + namespace + " to " + newValue);
    try {
        env.getConnector().namespaceOperations().setProperty(namespace, setting.property.getKey(),
                "" + newValue);
    } catch (AccumuloException ex) {
        if (ex.getCause() instanceof ThriftTableOperationException) {
            ThriftTableOperationException ttoe = (ThriftTableOperationException) ex.getCause();
            if (ttoe.type == TableOperationExceptionType.NAMESPACE_NOTFOUND)
                return;
        }
        throw ex;
    }
}